- Basic Metacharacters and Operators
- Anchors, Grouping, and Backreferences
- Miscellaneous Regular Expression Operators
Miscellaneous Regular Expression Operators
Binding Operators
Usage
expression =~ op expression !~ op
Description
The binding operators bind an expression to a pattern match or translation operator. Normally the m//, s///, and tr/// operators work on the variable $_. If you need to work on a variable other than $_, use the binding operator from before as follows:
$line=~s/^\s*//;
This causes the substitution operator to work on $line instead of $_. The return value for the operator on the right is returned by the bind operator.
The !~ operator works exactly the same as the =~ operator except that the return value is logically inverted. So, $f !~ /pat/ is the same as saying not $f =~ /path/.
Because =~ has a higher precedence than assignment, this allows you to do curious (and useful) things with the return value from =~. To return a list from a pattern match on $_, you would normally capture that as follows:
($first, $second)=m/(\w+)\W+(\w+)/;
With the bind operator, it's no different except that you can name your variable:
($first, $second)=$sentence=~m/(\w+)\W+(\w+)/;
Coupling this with the fact that the assignment operator yields an assignable value, you can assign, bind, and alter a variable at the same time:
# Okay, here's an assignment, bind and change. $orig="Won't see this trick in Teach Yourself Perl!"; ($lower=$orig)=~s/!$/ in 24 hours!/; # $lower is now "Won't see this [...] Yourself Perl in 24 Hours!" # Watch this: $changes=($upper=$lower)=~s/(\w\w+)/ucfirst $1/ge;
That last statement is kind of difficult and bears some explanation. The highest precedence operator in this expression is =~, but in order for the bind to happen, the ($upper=$lower) must be taken care of. So, $lower's value is assigned to $upper. The bind then takes $upper and performs the substitution. The substitution operator returns the number of substitutions made. This value passes back through the bind and is assigned to $changes. So $changes is 11 and $upper is "Won't See This Trick...".
A special note, if the thing to the right of the bind operator is an expression instead of a pattern match, substitution, or translation operator, a pattern match is performed using the expression.
$pattern="Buick"; if ($shorts =~ $pattern) { print "There's a Buick in your shorts\n"; }
Using the bind operator as an implicit pattern match is slower than explicitly calling m// because perl must re-compile the pattern for each pass through the expression.
NOTE
See Also
substitution operator, pattern match operator, and translation operator in this book
??
Usage
?pattern?modifiers
Description
The ?? operator works the same as the m// operator, with one small difference. The operator only attempts to match the pattern until it is successful and thereafter the operator no longer tries to match the pattern.
Each instance of the ?? operator maintains its own state. Once latched, the ?? can be reset by using the reset function. This resets all the ?? operators in the current package.
Example Listing 3.7
# Prints a summary of a given mailbox file. # Unix mailbox format is extremely common and uses a paragraph # beginning with "From " to describe the start of a message header. # The body of the message follows in subsequent paragraphs. use strict; use warnings; my($from, $subject, $to)=("","",""); open(MBOX, "mbox") || die; $/=""; # Paragraph mode. while(<MBOX>) { $from=$1 if (?^From: (.*)?m); $to=$1 if (?^To: (.*)?m); $subject=$1 if (?^Subject: (.*)?m); } continue { if (/^From/ or eof MBOX) { print "From: $from\nTo: $to\nSubject: $subject\n\n" if $from; # The 0-argument reset function resets all of the ?? # latches above for use in the next message. reset; $from=$subject=$to=""; } }
NOTE
See Also
reset, match operator, and match modifiers in this book
pos
Usage
pos pos target string
Description
The pos function returns the position in the target string where the last m//g left off. If no target string is specified, the target string $_ is used. The position returned is the one after the last match, so
$t="I am the very model of a modern major general with mojo"; $t=~m/mo\w+/g; print pos($t);
prints 19, which is the offset of the substring " of a modern...".
The pos function also can be assigned; doing so causes the position of the next match to begin at that point:
$t="I am the very model of a modern major general with mojo"; $t=~m/mo\w+/g; # Now we're at 19, just as before. pos($t)=38; # Skip forward to the word "general" $t=~m/(mo\w+)/g;# Grab the next "mo" word... print $1; # It's "mojo"!
Example Listing 3.8
# Sample from a text-processing system, where tags of the form # <#command> are substituted for variables, and other files can # be included, and so on. # pos() is used to return to the original matchpoint to re-insert # the new and improved text. use strict; # Just some sample data to play with. our $r="Hello, world"; my $data='bar<#var r/>Foo<#include "/etc/passwd"/>'; while($data=~/(<#(.*?)\/?>)/sg) { my($whole, $inside)=($1,$2); if ($inside=~/var\s+(\w+)/) { # Grab a variable from main:: no strict 'refs'; substr($data, pos($data)-length($whole), length($whole))=${'main::' . $1} } if ($inside=~/include\s+"(.*)"\s*/) { # Include another file.. open(NEWFH, $1) || die "Cannot open included file: $1"; { local $/; my $t=<NEWFH>; $t=eval "qq\\$t\\"; die "Inlcuded file $1 had eval error: $@" if $@; substr($data, pos($data)-length($whole), length($whole))=$t; } } # ...and many more } print $data; # Gives "barHello, worldFoo[contents of /etc/passwd]"
NOTE
See Also
match operator in this book
Translation Operator
Usage
tr/searchlist/replacement/modifiers y/searchlist/replacement/modifiers
Description
The tr/// operator is the translation (or transliteration) operator. Each character in searchlist is examined and replaced with the corresponding character from replacement. The tr/// operator returns the number of characters replaced or deleted. Similar to the match and substitution operators, the translation operator will use the $_ variable unless another variable is bound to it with =~:
tr/aeiou/AEIOU/; # Change $_ vowels to uppercase $t=~tr/AEIOU/aeiou/; # Change $t vowels to lowercase
The y/// operator is simply a synonym for the tr/// operator, and they are alike in every other respect.
The tr/// operator doesn't use regular expressions. The searchlist can be expressed as the following:
A sequence of characters, as in tr/aeiou/AEIOU/
A range of characters, similar to those used in character classes:
tr/a-zA-Z/n-za-mN-ZA-M/; # ROT-13 encoding
Special characters are allowed, such as backslash escape sequences (covered in the "Character Shorthand" section). Special characters that represent classes (\w\d\s) aren't allowed. (tr/// doesn't use regular expressions!)
No variable interpolation occurs within the tr/// operator. If a character is repeated more than once in the searchlist, only the first instance counts.
The replacement list specifies the character into which searchlist will be translated. If the replacement list is shorter than the searchlist, the last character in the replacement list is repeated. If the replacement list is empty, the searchlist is used as the replacement list (that is, the characters aren't changed, merely counted). If the replacement list is too long, the extra characters are ignored.
The modifiers are as follows:
Modifier |
Meaning |
/c | Compliments the search list. In other words, similar to using a ^ in a character class; all the characters not represented in the searchlist will be used. |
$consonants=$word=~tr/aeiouAEIOU//c; # Count consonants | |
/d | Deletes characters that are found, but doesn't appear in the replacement list. This bends the aforementioned rules about empty or too-short replacement lists. |
$text=~tr/.!?;://d; # Remove punctuation | |
/s | Takes repeated strings of characters and squashes them into a single instance of the character. For example, |
$a="Pardon me, boy. Is that the Chattanooga Choo-Choo?" | |
$a=~tr/a-z A-Z//s; # Pardon me, boy. Is that the Chatanoga Cho-Cho? |
NOTE
See Also
character shorthand and character classes in this book
study
Usage
study study expression
Description
The study function is a potential optimization for perl's regular expression engine. It prepares an expression (or $_ if none is specified) for pattern matching with m// or s///. It does this by prescanning the expression and building a list of uncommon characters seen in the expression, so that the match operators jump right to them as anchors.
Calling the study function for a second expression undoes any optimizations by the previously studied expression.
Whether study will save any time on your regular expression matches depends on several factors:
The study process itself takes time.
The kinds of data that makes up the expression being studied.
Whether your search expression uses many constant strings (study might help) or few constant strings (study might not help).
As always, with any optimization, use the Benchmark module and determine whether there really is a cost savings to using study. Constructing a case in which study is actually useful is difficult. Do not use it indiscriminately.
NOTE
See Also
qr in this book
Quote Regular Expression Operator
Usage
qr/pattern/
Description
The qr operator takes a regular expression and precompiles it for later matching. The compiled expression then can be used as a part of other regular expressions. For example,
$r=qr/\d{3}-\d{2}-\d{4} $name/i; if (/$r/) { # Matched digits-digits-digits and whatever was in $name... }
Similar to the match operator, the delimiters can be changed to any character other than whitespace. Also, using single quotes as delimiters prevents interpolation.
Example Listing 3.9
# A short demo of the qr// operator. The fast subroutine # runs nearly 4 times faster than the slow subroutine # because the qr// operator pre-compiles all of the regular # expressions for &fast. # Remember, if you're not sure something is faster: Benchmark it. use Benchmark; sub slow { seek(BIG, 0, 0); @pats=qw(the a an); while(<BIG>) { for (@pats) { if (/\b$_\b/i) { $count{$_}++; } } } } sub fast { seek(BIG, 0, 0); # Pre-compile all of the patterns with # qr// @pats=map { qr/\b$_\b/i } qw(the a an); while(<BIG>) { for (@pats) { if (/$_/) { $count{$_}++; } } } } open(BIG, "bigfile.txt") || die; timethese(10, { slow => \&slow, fast => \&fast, });
NOTE
See Also
match modifiers in this book