Perl Regular Expression
Operators, or Functions
string =~ regexpr |
Is true if regexpr matches in string,
which could be a constant or variable. |
while ($line=) {
if ($line =~ /http:/) {
print $line; # print URLs
}
}
Alternately, using the default space $_ if =~ not used:
while () {
print if /http:/;
}
"split /regex/, string" splits "string" into a list of substrings and returns
that list. The "regex" determines the character sequence that "string" is
split with respect to. For example:
$x = Calvin and Hobbes";
@word = split /\s+/, $x;
yields:
$word[0] = 'Calvin'
$word[1] = 'and'
$word[2] = 'Hobbes'
If the empty regex "//" is used, the string is s plit into individual characters.
If the regex has groupings, then the list produced contains the matched substrings
from groupings as well:
$y = "/usr/bin";
@parts = split m!(/)!, $y;
$parts[0] = '' # since first character matches!
$parts[1] = '/'
$parts[2] = 'usr'
$parts[3] = '/'
$parts[4] = 'bin'
Examples:
- /[+-]?\d+/ # matches integers with optional sign
- /[+-]?(\d+\.d+|\d+\.|\.\d+)/ # matches floating point, no exponent
Note importance of putting "\d+\.\d" before "\d+\." which would otherwise
match first and ignore fractional part.
- /[+-]?(\d+\.d+|\d+\.|\.\d+|\d+)([eE]?\d+)?/ # matches floating point
Since white space is errelevent unless explicity put in, this rexex can be writen as:
/[-]?\ * # match an optional sign with optional spaces
( # then match integers of fp mantissa
\d+\.\d+ # mantiss of form: a.b
\d+\. # mantiss of form: a.
\.\d+ # mantiss of form: .b
\d+ # mantiss of form: a
)
([eE][+-]?\d+)? # optional exponent
/x # note 'x' modifier to allow spaces