Project Advice

Project Advice

3. Regular expressions

3.2 Special characters

Special characters are things like \i (increment), which puts a number into your replacement expression (we saw this with our first regular expression example, used for paragraph numbering). You can, of course, control which number the incrementing starts with, the step, and the direction on the number line. So you could number your matches 100, 110, 120, etc., or decrement them as, say, 99, 97, 95...

Also useful are ^ and $, which match the beginning and end of a line, respectively, and . which matches any character, and \n and \t, which match a line break and a tab, respectively.

Regular expression syntax does tend to vary slightly, so it's best to check your editor's documentation. For example, a distinction some editors make is between whether a match is 'lazy' or 'greedy'. Lazy matches match the minimum possible match, while greedy matches match as much as possible. Lazy matches are usually safer: greedy matches, like the Tiger Who Came to Tea, can end up devouring far more than you were expecting.