# Effective ways of teaching regex to students who know Java

My students are knowledgeable about Java but need to know something about Regular Expressions. Many students find them difficult and intimidating. The students don't need to know every detail, but do need to know enough to work effectively and learn more.

What are effective ways of teaching the fundamentals of regular expressions in Java or otherwise? I'd like the students to understand how it works, and not only what each pattern does.

This year it's been decided at my school (I'm a teacher assistant this year) to teach the students a bit about regular expressions. We teach in Java. The students have never faced anything like regex in the past. The students I've asked (we have a test group - we test new ideas on them for feedback. A diverse group in terms of level and background1 consisting of 3-4 students) seem to find regex very difficult (we just gave them the pattern list and explain each pattern).

1background in terms of knowledge in the field.

• This is too small for an answer but start by showing them why regular expressions are worth their time to learn. I found when I was learning Regex, it was a horrid experience until I found use cases where they made my life easy enough to be worth the cost of learning. – Cort Ammon - Reinstate Monica Aug 6 '17 at 18:31
• @CortAmmon good point. – ItamarG3 Aug 6 '17 at 18:32
• Regex is great when you are creating a language-using tool, or something that processes stuff written in a (regular) language. Not much use for them otherwise. Great for Domain Specific Languages. Why do they need to learn them? Maybe they need background in language design first? – user737 Aug 6 '17 at 20:39

Many students find them difficult and intimidating.

Then the challenge is to make them fun and engaging and (ideally) interactive. I myself have undertaken the challenge here and there of teaching myself regex, and two resources rose to the top in my own learning: RegexOne and Regex Crossword.

RegexOne is great as a lesson-by-lesson tutorial. Students can type and see in real time what tests cases their regex does/does not match. There are 15.5 lessons and 8 practice problems. The practice problems draw from relevant use cases, such as extracting a phone number, trimming white space, or reading a log file.

Regex Crossword mixes regex with logic: students have to fill in a square grid and track how gradually more complex regular expressions interact with each other. (That sounds a bit vague, but an example like this one makes it clear what I mean.) The crosswords tend to have fun themes/solutions. There are many crosswords to solve, including ones with great complexity, so students will have to spend lots of time working at it if they'd like to complete all the puzzles.

When it comes to working in class, it'd be pretty easy to work with some of the smaller crosswords in a non-digital manner. You could put one on the board at the start of every class as a warm-up or print/copy some of your favorite from the site. Maybe even have students work in groups and complete to solve a series of puzzles in the fastest possible time.

Between these two resources, students will at least have a pretty good start to working with regex.

• In a similar way you could create a Jeopardy game. The Jeopardy "answers" are REs. The contestants are given an RE for some category and the first to "ring the bell" gets to reply with a valid match - in the form of a question, of course. I used to keep a set of hotel desk bells for this sort of thing. This only works for deciphering/reading, of course. As you proceed through a category the REs get harder. The categories can be RE types or something more elaborate. – Buffy Aug 5 '17 at 22:06
• A similar activity to RegexOne, without the tutorial text but with larger test suites for each problem, is alf.nu/RegexGolf – Peter Taylor Aug 10 '17 at 7:30

In Java: Don't

In programming language X: Don't

Treat Regular Expression as its own language. Teach regex in a language-agnostic fashion using tools that process regular expressions rather than trying to teach regular expressions and their implementation in Java, or any other language. Each language has its own, idiosyncratic, engine and method of use. Different engines implement subsets of the entirety of the regular expression specifications.

As a starting point, use an online version of a regular expression editor. There are two fairly good versions that I'm aware of. First is Regex101, which has a decent IDE style interface, including an live explanation of the current expression, and a token list to choose from. The one feature I like is that it allows you to "Save & Share" the expression, which adds it to a library of expressions available to others. Of course, that library is also available to you, so you can try to find one that's already written to do what you need. One major issue I have with this site is that it still does not implement the entire specification of regular expressions. Specifically, it does not handle If-Then-Else conditionals: (?(subpattern)yes|no) and (?(group)yes|no).

A second site, which will handle the If-Then-Else conditionals, is Regex Storm. This one produces a nice set of tables for the "output" of the expression, but lacks the collection of tokens, and library of expressions of Regex101. As near as I have been able to determine, this editor implements the entire set of regular expressions available in all engines extant, including left-to-right text. There is also an extensive reference on this site that can be terse at times, but does include examples for most of the complex, or potentially confusing, constructs.

After selecting the editor to use, local, online, etc., the problem now becomes how to work through the "language" of regex. I don't like to reinvent the wheel. Therefore, the best option I can think of is to follow the outline of the regex tutorial built in to Perl, "perlretut", which should be available from any complete Perl install by typing perdoc perlretut on the command line. Perl.org's documentation also has it online in HTML for all to use. Granted, all the samples there are given in the context of use within Perl, so they need to be converted for your classroom use. The idea isn't to, necessarily, use their examples, as to follow the sequence they have created. They've invented the wheel, now you get to design the rims and hub caps so that the vehicle you need is "road ready."

To cover the very basic outline, below is the relevant sections from the Table of Contents for perlretut. For the "died-in-the-wool" purists, it even begins with the inexplicably obligatory "Hello World" of program language instruction.

• Part 1: The basics
• Simple word matching
• Using character classes
• Matching this or that
• Grouping things and hierarchical matching
• Extracting matches
• Backreferences
• Relative backreferences
• Named backreferences
• Alternative capture group numbering
• Position information
• Non-capturing groupings
• Matching repetitions
• Possessive quantifiers
• Building a regexp
• Using regular expressions in Perl
• Part 2: Power tools
• More on characters, strings, and character classes
• Compiling and saving regular expressions
• Composing regular expressions at runtime
• Embedding comments and modifiers in a regular expression
• Looking ahead and looking behind
• Using independent subexpressions to prevent backtracking
• Conditional expressions
• Defining named patterns
• Recursive patterns
• A bit of magic: executing Perl code in a regular expression
• Backtracking control verbs
• Pragmas and debugging

One additional resource which I have found helpful from time to time is Regular-Expressions.info. They include a reference and a tutorial, among other things. The significance of this site is that it often has a different way of "saying" the same thing as other sites, which can reach a student when the standard explanation just does not "click" for the student.

• Most importantly: call them regex or regexp, not regular expressions, or the person who later teaches them the Chomsky hierarchy will curse you from the depths of their heart. – Peter Taylor Aug 10 '17 at 7:23

First and foremost, regular expression is a mathematical concept. Lacking the relevant background and intuition does make it intimidating indeed. Students must have at least a vague understanding of what FA is, and what makes it F. IMO, understanding the abilities and limitations of FAs is the key.

I found that conducting a creation of an regexp interpreter is an eye opener. An ultimate goal of the exercise is to make a grep clone.

Start simple, from strcmp. Introduce a dot. Spend some time explaining what makes it a meta character, and how to tell a meta from a normal character. Introduce a star. Say few words on Kleene theorem, and on how this innocent star is really a centerpiece of a profound result.

At this point students shall have enough intuition to grasp the rest of the syntactic sugar.

• I dont even know what you mean by FA or that being F, yet I'm pretty good with regex. Similarly, I don't understand why you think math is needed to use, or understand, them. They're pattern matching, right? Not formula matching. – Gypsy Spellweaver Aug 6 '17 at 0:18
• @GypsySpellweaver FA is finite automaton, where F stands for finite. – user58697 Aug 6 '17 at 0:24
• So, how is knowing any of that important to using regex, in programming or CLI? In implementing a regex interpreter, that might be important, can't say, but that's not part of the question from the OP. – Gypsy Spellweaver Aug 6 '17 at 0:33
• @GypsySpellweaver OP didn't ask of using regex; the question is explicitly about fundamentals and understanding how it works. You cannot teach fundamentals without resorting to fundamentals. – user58697 Aug 6 '17 at 0:38
• @GypsySpellweaver I agree on the misreading. OTOH I have seen enough senior programmers trying to parse stack languages with regexes. I am sure you did too. I do want my students to know what they are doing. – user58697 Aug 6 '17 at 0:53

It is important to know that there are two languages involved, each with its own rules and metacharacters.

Characterclassese is the language of character classes, which are character wildcards. Its special characters include [ ] ^-. Every character class stands for a single character. Literal characters are themselves.

Regexese is a second language. Its atoms are character classes. Here are some of its rules

• Juxaposition means "and then immediately."
• It has an operator for or which is |
• Parentheses are delimiters and can override the order of operations
• There are several postfix multiplicity operators which have higher precedence than juxtaposition.
• Special character classes include .^\$ These are metacharacters in the language as well.

It is important to point out the existence of the two layers and how they work together. "Hedwig," the O'Reilly book on Regexes is great.