bsdjunkie
June 3rd, 2003, 14:43
A few good ones taken from the Perl Cookbook:
swap first two words:
s/(\S+)(\s+)(\S+)/$3$2$1/
MM/DD/YY HH:MM:SS
m|(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)|
Deleting C Comments:
s{
/\* #match opening delimiter
.*? #match minimal number of chars
\*/ #match closing delimiter
} []gsx;
Removing leading and trailing white space:
s/^\s+//;
s/\s+$//;
Dotted quads (most ip addresses):
m{
^ ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
$
}x
Remove leading path from filename:
s(^.*/)()
Joining continuation lines in multiline string:
s/\n\s+/ /g
Finding all CAPS words:
@capwords = m/(\b[^\Wa-z0-9_]+\b)/g;
Finding all lowercase words:
@lowords = m/(\b[^\WA-Z0-9_]+\b)/g;
Finding links in simple html:
@links = m/<A[^>]+?HREF\s*=["']?([^'">]+?)[ '"]?>/sig;
YYYY-MM-DD:
m/(\d{4})-(\d\d)-(\d\d)/
North America Telephone numbers:
m/ ^
(?:
1 \s (?: \d\d\d \s)? #1, or 1 and area code
| #or
\(\d\d\d\) \s #area code with params
|
(?: \+\d\d?\d? \s)? #option +country code
\d\d\d ([\s|\1) #and area code
)
\d\d\d (\s|\1) #perfix (and area code seperator)
\d\d\d\d #exchange
$
/x
swap first two words:
s/(\S+)(\s+)(\S+)/$3$2$1/
MM/DD/YY HH:MM:SS
m|(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)|
Deleting C Comments:
s{
/\* #match opening delimiter
.*? #match minimal number of chars
\*/ #match closing delimiter
} []gsx;
Removing leading and trailing white space:
s/^\s+//;
s/\s+$//;
Dotted quads (most ip addresses):
m{
^ ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
$
}x
Remove leading path from filename:
s(^.*/)()
Joining continuation lines in multiline string:
s/\n\s+/ /g
Finding all CAPS words:
@capwords = m/(\b[^\Wa-z0-9_]+\b)/g;
Finding all lowercase words:
@lowords = m/(\b[^\WA-Z0-9_]+\b)/g;
Finding links in simple html:
@links = m/<A[^>]+?HREF\s*=["']?([^'">]+?)[ '"]?>/sig;
YYYY-MM-DD:
m/(\d{4})-(\d\d)-(\d\d)/
North America Telephone numbers:
m/ ^
(?:
1 \s (?: \d\d\d \s)? #1, or 1 and area code
| #or
\(\d\d\d\) \s #area code with params
|
(?: \+\d\d?\d? \s)? #option +country code
\d\d\d ([\s|\1) #and area code
)
\d\d\d (\s|\1) #perfix (and area code seperator)
\d\d\d\d #exchange
$
/x