Regular Expressions   «Prev 

Perl Delimiter Example

Preserving case in Perl

If the goal is to replace occurrences of alot with a lot, why not just do it this way?
s/alot/a lot/ig

Using the subexpressions will preserve the case; the method above will make every occurrence all lowercase.
Using subexpressions is usually preferable because it is more general:
s/(a)(lot)/$1 $2/ig


Named Subexpressions (5.10)

If you use Perl 5.10 or better, you can also use named subexpressions. Ordinarily, you refer to a captured group in the regex with \1, \2, and so on. After a successful match, those are $1, $2, and so on. With named subexpressions you can name them and make things easier to read. To name a subexpression, use the syntax (? <name>...).
To refer to it again inside of the regex, use
\g{name}. 

To refer to the match outside of the regex, be aware that it's a key in the special %+ hash. For example, the double-word stripper would look something like this:
The %+ hash is a special variable that contains only entries for the last successfully matched named subexpressions in the current scope. Thus, if a named subexpression fails to match, it will not have an entry in the %+ hash. There is a corresponding %- hash not covered here.

Using independent subexpressions to prevent backtracking

Independent subexpressions (or atomic subexpressions) are regular expressions, within the context of a larger regular expression, which function independently of the larger regular expression. That is, they consume as much or as little of the string as they wish without regard for the ability of the larger regexp to match. Independent subexpressions are represented by (?>regexp) or (starting in 5.32, experimentally in 5.28) (*atomic:regexp). We can illustrate their behavior by first considering an ordinary regexp:

$x = "ab";
$x =~ /a*ab/;  # matches