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.