Here is my vision. Please entertain it, if you would. If you haven't been able to tell, I'm serious, and I'm a regex fanatic.
Learning Perl's Regular Expressions
- Familiarize the reader with the tasks that regular expressions are intended to complete.
- What is a regular expression (regex)?
- What can they do?
- What are some practical uses for them?
- What are some impractical uses for them?
- How can they solve daily programming problems?
- Abstract thinking about regexes.
- How to explain a regex's purpose.
- How to tackle a given parsing problem with a regex.
- How to say what you mean, and mean what you say.
- What else is out there, apart from regexes.
- Simple patterns.
- Plain text: /hello world/
- Safe patterns: /hello \Q$place\E/
- Character classes: /[Hh]ello [Ww]orld/
- Case-insensitivity: /hello world/i
- Alternation: /hello|goodbye/
- Matching something other than $_: $foo =~ /hello/
- More "patterny" patterns.
- Dot: /h.ll/
- Quantifiers: /hello(?: +world)?/
- Macros: /hello\s+world/
- Anchors: /^hello,?\s+world$/
- Alternate m// characters: m!a/b/c!
- Getting something back.
- Capturing: /hello,?\s+(\w+)/
- Back-references: /\b(\w)\w+(\1)\b/
- Global matching: /(\d+)/g
- Regex-y functions: split(), grep(), map()
- Useful variables: $1, @-, @+
- Not-so-useful variables: $`, $&, $'
- More modifiers.
- Modifying .: /a.b/s
- Modifying ^ and $: /^foo$/m
- Explaining your regex using /x
- Using qr// and /o
- Breakpoint: greediness and backtracking
- What is backtracking, and how does it work?
- What is greediness, and how do you avoid it?
- Minimal (lazy) vs. maximal (greedy): /a\w+?z/
- Understanding the "left-most longest" rule.
- Substitution.
- Using s///.
- Calling functions with /e.
- Simple assertions.
- Positive look-ahead: /foo(?=bar)/
- Negative look-ahead: /foo(?!bar)/
- Common traps: "aaabar" =~ /a+(?!bar)/
- More assertions.
- Positive look-behind: /(?<=foo)bar/
- Negative look-behind: /(?<!foo)bar/
- The constant-width assertion plague: /(?<=ab+a)/
- The cut assertion: /(?>a+)a/
- Logic-flow in regexes.
- The conditional assertion: /(?(...)a|b)/
- Embedding code.
- Advanced use of the /e modifier.
- The evaluate assertion: /(?{ code })/
- The delayed regex assertion: /(??{ regex })/
- Advanced global searching.
- The pos() function
- The \G anchor and the /gc modifiers.
- Common mistakes.
- $&, et. al.
- Problems with /$foo/.
- Matching too much.
- Wasteful modifiers.
- Optimizing your regexes.
- "Death to .*."
- Match only what you want.
- Unrolling the loop.
- Concrete applications of regexes.
- Data transformation for sorting.
- Matching in reverse ("sexeger").
- Regex tools and resources.
Please give me your input. Is this too much for a tutorial? I want it to be in the form of Learning Perl, with exercises and such, but I want it to include as much as it should so that one has a firm grasp on Perl's regexes.
japhy --
Perl and Regex Hacker