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

stdnrw
June 3rd, 2003, 17:11
.

hoserian
June 8th, 2003, 01:09
for making your own regular expressions, and getting good at it (which i'm not) O'Reilly's Mastering Regular Expressions comes highly recommended.

It's available on bookpool for about $25, and from what i've been told by anyone who's read it, it's worth the money