pattern matching

home button  back button

Pattern Matching



PatternResult
.Matches any character except newline
[a-z0-9]Matches any single character of set
[^a-z0-9]Matches any single character not in set
\dMatches a digit, same as [0-9]
\DMatches a non-digit, same as [^0-9]
\wMatches an alphanumeric (word) character [a-zA-Z0-9_]
\WMatches a non-word character [^a-zA-Z0-9_]
\sMatches a whitespace character (space, tab, newline...)
\SMatches a non-whitespace character
  
\nMatches a newline
\rMatches a return
\tMatches a tab
\fMatches a formfeed
\bMatches a backspace (inside [] only)
\0Matches a null character
\000Also matches a null character because ...
\nnnMatches an ASCII character of that octal value
\xnnMatches an ASCII character of that hexadicimal value
\cXMatches an ASCII control character
\metacharMatches the character itself (\|,\.,\*...)
  
(abc)Rembers the match for later backreferences
\1Matches whatever the first set of parens matched
\2Matches whatever the second set of parens matched
\3 and so on...
  
x?Matches 0 or 1 x's, where x is any of the above
x*Matches 0 or more x's
x+Matches 1 or more x's
x{m,n}Matches at least m x's but nore more than n
  
abcMatches all of a, b, and c in order
fee|fie|foeMatches one of fee, fie, or foe
  
\bMatches a word boundry (outside [ ] only)
\BMatches a non-word boundry
^Anchors match to the beginning of a line or string
$Anchors match to the end of a line or string
Pattern Binding Operator

The pattern binding operator (=~) is described in perlop.pod:

Binary "=~" binds a scalar expression to a pattern match. Certain operations search or modify the string $_ by default. This operator makes that kind of operation work on some other string. The right argument is a search pattern, substitution, or translation. The left argument is what is supposed to be searched, substituted, or translated instead of the default $_. The return value indicates the success of the operation.

The binding operator works on that part of the scalar that matches the pattern in the match, substitution or translation, changing it "in place." Example:

    $data =~ s/"//g;

This statement searches the scalar $data for all double-quotes (via 'g' switch) and removes them (i.e., replacing with nothing.) The value in $data then has no double-quotes. When the description speaks of a 'return value' as successful or not, it can be used in conditional tests. For example:

    if ($data =~ s/"//g){print "Success\n";}
       or
    $success = $data =~ s/"//g;

The first would print "Success" if pattern found and substitution made. The variable $success would contain a true value if the double-quote character is found. When used with substitution, it would contain the number of substitutions made. (Other right side operations return other values, if successful.)




This is designed to be an accessible website, please report any problems. by submitting this form.



Valid XHTML 1.0 Transitional link to w3.org validator Valid CSS link to w3.org validator the W3C Link Checker

See the PHP source of this page, here.

Copyright 1991-2011 and hand coded by James Lockie.
Last updated May 20 2012.