Regular Expressions   «Prev 

Using two Sub-expressions in Perl

Here is an example of using two subexpressions:
while ($number =~ s/(.*\d)(\d\d\d)/$1,$2/g) { }

Beginning with Perl version 5.5, we can compile regular expressions and keep references to them without using a match or substitution operator. We can do all of our regular expression handling and preparation before we actually want to use them. Since a regular expression reference is just a scalar like any other reference, we can store it in an array or hash, pass it as an argument, interpolate it into a string, or use it in the many other ways we can use a scalar.

1) Matches a minimum of 4 numbers in a row
1) Matches a minimum of 4 numbers in a row

2) First iteration through the regular expression
2) First iteration through the regular expression


3) Second iteration through the regular expression
3) Second iteration through the regular expression

4) Third iteration through the regular expression
4) Third iteration through the regular expression