Regular Expressions   «Prev  Next»
Lesson 12 Extended regular expressions
Objective Rewrite one of the examples that you wrote for a previous lesson so it uses the /x modifier.

Perl Extended Regular Expressions

More often than not, people do not like regular expressions because they are ugly, terse, cryptic-looking, and reminiscent of what happens to a terminal that is connected to a 300 baud modem during a thunderstorm.
Beginning with version 5, and refined in 5.002, Perl's regular expressions allow the use of whitespace and comments to make them more acceptable in combination with one another.
This extension is invoked with the /x modifier, and it makes the regex ignore all whitespace and Perl comments.
For example, the following substitution was in the solution to the exercise that had you inserting Re: at the beginning of an email Subject: header and not add a second Re: if one is already there.

The example below would normally appear on a single line. It is broken into multiple lines for readability.
s/^Subject:\s*(Re:\s*)?(.*)
 /Subject: Re: $2/i; 


It might be easier to follow what is going on here if we could break this into separate lines and add comments. That is what the /x modifier allows us to do.
s/
^Subject:
\s*
(Re:\s*)? 
  # match "Re: " if present
(.*)
  # match 'anything else'
  # is the subject
/Subject: Re: $2/ix;
  # put 'anything else' in
  # after "Re: "
  # x allows us to split this
  # into multiple lines

You need not always spread it across several lines and insert a lot of comments. Sometimes it is just enough to spread out the expression a little with some extra whitespace. Here is our example that puts commas in a number from a previous
while ($number =~ s/ ( .*\d ) ( \d \d \d ) /$1,$2/xg) { }

Spaces are not ignored in the right-hand side replacement expression.

Extended Regular Expressions - Exercise

Click the exercise link below to rework one of your previous exercise answers using the /x modifier.
Extended Regular Expressions - Exercise