|
This is a nice, non-threatening intro. One piece of advice that I've learned from teaching people regex syntax is that it's much easier to keep it to three basic topics at first (Repetition, Alternation, Concatenation), and then describe the rest of the stuff (character classes, character escapes, etc.) as "syntactic sugar" that makes those previous topics simpler, or provides more power. I usually introduce groups pretty early but most people get them notionally because they're kind of like algebraic parentheses. And then I'll expand on groups as well to show more power, escapes, etc. For example, (a|c|d|e|f|g|...|z) uses only notional groups and basic alternation while [abcdefghi...xyz] shows character classes, and [a-z] shows ranges - each step builds on the previous step and shows how to make them easier. For the learner this seems to act as building blocks rather than "separate things that are kind of alike I need to learn" This is similar to how you can talk about repetition as aaaaa, then aaaaaaaaaaaaa, then a, then aa, then a+, then a?, a{0,1}, a{0,5}, a{1,5}, a{,10}, etc. which simplifies, then generalizes the idea of repetition from a very natural concept build on concatenation to an opaque looking syntax that turns out to be both general and powerful After that, most of the time I need to explain how capturing works, and how to turn it off and so on. Good tools help here and it starts to move away from a whiteboard exercise into something more active. But if students have followed you to this point it starts to make them feel very powerful as they're suddenly parsing things apart and transforming them. At the end I usually follow up with a big on anchors (^ and $) and other odds and ends (case insensitivity, global search, greedy and non-greedy, etc.) and usually turn people loose after that. I've rarely found people who actually need lookarounds and other advanced topics and those are usually covered one-on-one as they need. But this is fairly minor quibbling and is just rearrangement of what's here. I think this is overall a nice clear explanation. Regex syntax is honestly pretty simple once the syntax magic is explained. What I think would be really helpful is a tool where somebody can type in a regex, have it checked for syntax and then generate the list of strings that would match it (within the constraints of limits on infinite repetition operators, like turning * to {0,2} or something. |