Node:Regular Expression Shortcuts, Previous:Pattern Matching, Up:Regular Expressions



Regular Expression Shortcuts

Writing out regular expressions can be problematic. For example, if we want to have a regular expression that matches all digits, we have to write:

     (0|1|2|3|4|5|6|7|8|9)
     

It would be terribly annoying to have to write such things out. So, Perl gives an incredible number of shortcuts for writing regular expressions. These are largely syntactic sugar, since we could write out regular expressions in the same way we did above. However, that is too cumbersome.

For example, for ranges of values, we can use the brackets, []'s. So, for our digit expression above, we can write [0-9]. In fact, it is even easier in perl, because \d will match that very same thing.

There are lots of these kinds of shortcuts. They are listed in the perlre online manual. They are listed in many places, so there is no need to list them again here.

However, as you learn about all the regular expression shortcuts, remember that they can all be reduced to the original operators we discussed above. They are simply short ways of saying things that can be built with regular characters, *, (), and |.