***** infoCopter.com/perl *****
Regular Expressions
Syntax of regular expressions in Perl
Common needed regular expressions
This page describes the syntax of regular expressions in Perl. For a
description of how to use regular expressions in matching
operations, plus various examples of the same, see discussions
of m//, s///, qr// and ?? in perlop/"Regexp Quote-Like Operators".
Matching operations can have various modifiers. Modifiers
that relate to the interpretation of the regular expression inside
are listed below. Modifiers that alter the way a regular expression
is used by Perl are detailed in perlop/"Regexp Quote-Like Operators" and
perlop/"Gory details of parsing quoted constructs".
- i
-
Do case-insensitive pattern matching.
If use locale is in effect, the case map is taken from the current
locale. See perllocale.
- m
-
Treat string as multiple lines. That is, change "^" and "$" from matching
the start or end of the string to matching the start or end of any
line anywhere within the string.
- s
-
Treat string as single line. That is, change "." to match any character
whatsoever, even a newline, which normally it would not match.
The /s and /m modifiers both override the $* setting. That
is, no matter what $* contains, /s without /m will force
"^" to match only at the beginning of the string and "$" to match
only at the end (or just before a newline at the end) of the string.
Together, as /ms, they let the "." match any character whatsoever,
while yet allowing "^" and "$" to match, respectively, just after
and just before newlines within the string.
- x
-
Extend your pattern's legibility by permitting whitespace and comments.
These are usually written as "the /x modifier", even though the delimiter
in question might not really be a slash. Any of these
modifiers may also be embedded within the regular expression itself using
the (?...) construct. See below.
The /x modifier itself needs a little more explanation. It tells
the regular expression parser to ignore whitespace that is neither
backslashed nor within a character class. You can use this to break up
your regular expression into (slightly) more readable parts. The #
character is also treated as a metacharacter introducing a comment,
just as in ordinary Perl code. This also means that if you want real
whitespace or # characters in the pattern (outside a character
class, where they are unaffected by /x), that you'll either have to
escape them or encode them using octal or hex escapes. Taken together,
these features go a long way towards making Perl's regular expressions
more readable. Note that you have to be careful not to include the
pattern delimiter in the comment--perl has no way of knowing you did
not intend to close the pattern early. See the C-comment deletion code
in perlop.
The patterns used in Perl pattern matching derive from supplied in
the Version 8 regex routines. (The routines are derived
(distantly) from Henry Spencer's freely redistributable reimplementation
of the V8 routines.) See Version 8 Regular Expressions for
details.
In particular the following metacharacters have their standard egrep-ish
meanings:
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class |
|
By default, the "^" character is guaranteed to match only the
beginning of the string, the "$" character only the end (or before the
newline at the end), and Perl does certain optimizations with the
assumption that the string contains only one line. Embedded newlines
will not be matched by "^" or "$". You may, however, wish to treat a
string as a multi-line buffer, such that the "^" will match after any
newline within the string, and "$" will match before any newline. At the
cost of a little more overhead, you can do this by using the /m modifier
on the pattern match operator. (Older programs did this by setting $*,
but this practice is now deprecated.)
To simplify multi-line substitutions, the "." character never matches a
newline unless you use the /s modifier, which in effect tells Perl to pretend
the string is a single line--even if it isn't. The /s modifier also
overrides the setting of $*, in case you have some (badly behaved) older
code that sets it in another module.
The following standard quantifiers are recognized:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times |
|
(If a curly bracket occurs in any other context, it is treated
as a regular character.) The "*" modifier is equivalent to {0,}, the "+"
modifier to {1,}, and the "?" modifier to {0,1}. n and m are limited
to integral values less than a preset limit defined when perl is built.
This is usually 32766 on the most common platforms. The actual limit can
be seen in the error message generated by code such as this:
$_ **= $_ , / {$_} / for 2 .. 42; |
|
By default, a quantified subpattern is "greedy", that is, it will match as
many times as possible (given a particular starting location) while still
allowing the rest of the pattern to match. If you want it to match the
minimum number of times possible, follow the quantifier with a "?". Note
that the meanings don't change, just the "greediness":
*? Match 0 or more times
+? Match 1 or more times
?? Match 0 or 1 time
{n}? Match exactly n times
{n,}? Match at least n times
{n,m}? Match at least n but not more than m times |
|
Because patterns are processed as double quoted strings, the following
also work:
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\x{263a} wide hex char (Unicode SMILEY)
\c[ control char
\N{name} named char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E |
|
Common needed regular expressions
- Remove leading and trailing spaces (trim a string)
Actually, it's a work-around, but it works properly and quickly ;-)
$trimmed_string = ' Firstname Middle. Lastname ';
$trimmed_string = join " ", grep { $_ } split / /, $trimmed_string;
Perl pre-parsed version would be:
$trimmed_string = join(' ', grep({$_;} split(/ /, $trimmed_string, 0)));
- Remove HTML tags
$Msg =~ s/<[^>]+>//g;
- Remove dot dirs out of directory listing
opendir(D, '/var/www/') or print $!;
while (my $f = readdir(D)) {
next if $f =~ /^\.+$/;
print "$f\n";
}
closedir(D);
- Simple check for valid E-Mail address
/^[A-Z0-9\-_\.]+\@[A-Z0-9\-\.]+\.[A-Z]{2,}$/i
- Simple check for a valid 2nd level domain
/^[a-z0-9\-]+\.[a-z]{2,5}$/i
Better check for domains:
@answer = split /\n/, `whois -h whois.nic.ch $domain` if $domain =~ /\.ch$/i;
@answer = split /\n/, `whois $domain` if $domain !~ /\.ch$/i;
- Extract a second-level domain e-mail address from a text line
email or abuse inquiries contact postmaster@mail.com. law enforcement issues contact 646-223-1227
after: postmaster@mail.com
$line =~ s/.*\s([a-z0-9\-_\.]+\@[a-z0-9\-\.]+\.?[a-z]{2,}).*/$1/;
- Check if the file has a suffix ".htm" or ".html"
/.+\.html?$/i;
- Check, if ending of a string meets a list of suffixes
if ($file =~ /(\.jpg)|(\.gif)$/i) {
print "Grafik\n";
}
|