View Single Post
  #2 (permalink)  
Old 06-29-09, 06:06 PM
Boraan's Avatar
Boraan Boraan is offline
Coding Addict
 
Join Date: Jul 2007
Location: Clayton, NC
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
Regular expressions. When I was learning Perl this was drilled into my head. Memorize to be a pro.

Expressions (symbol - match):
. - matches any single character except newline
[a-z0-9] - matches any single character of set
[^a-z0-9] - matches any single character NOT in set
\d - matches a digit (a number 0-9)
\D - matches a non-digit (anything but 0-9)
\w - matches an alphanumeric character (a-zA-Z0-9 and _)
\W - matches a non-alphanumeric character
\s - matches a whitespace character (space, tab, newline)
\S - matches a non-whitespace character
\n - matches a newline
\r - matches a return
\t - matches a tab
\f - matches a formfeed
\b - matches a backspace (inside [ ] brackets only)
\0 - matches a null character
\b - matches a word boundary (outside [ ] brakets only)
\B - matches a non-word boundary

Placement:
^ - anchors match to the beginning of a line or string
$ - anchors match to the end of a line or string

Equations:
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 no more than n

(pat1|pat2) - matches either pat1 or pat2
(pat) - stores a pattern for backreferencing via $1...$9

and a crucial one for returning matches to print... $&

All other character are self matching, for example a will match a. The special characters that you have to backslash are + ? . * ^ $ @ ( ) [ ] | \. So you when you want to match the @ in an email would be \@

Regualar expression work to match anything you need like so

$var =~ /pattern/ OR $var =~ m/pattern/ (m#pattern#) works well.

regexps can be used to replace patterns as well.
$var =~ s/pattern/replacement/;

so a numeric would be $var =~ [\d]; as it matches 0-9 in that set. same goes for your other sets.
lower case alpha $var =~ [a-z];
upper case alpha $var =~ [A-Z];
lower alphanumeric $var =~ [a-z0-9];
upper alphanumeric $var =~ [A-Z0-9];

Remember that perl is cAsE sEnSiTiVe. matching with \w will match anything a-zA-Z0-9 and _. $var =~ [\w\];
for it to be insensitive you would use i modifier like so.
$var =~ [\w\]/i;

matches can also be global with g. $var =~ [\w\]/g;

A good example would be an email address. To properly match on you would use something like this.
$var =~ /[\w\-]+\@[\w\-]+[\w\-]+/

you should take the time to memorize expressions and how they are used. That's half the perl battle if you can master those.

Cheers!
__________________
Dexter Nelson
Techdex Development & Solutions
========================
Message Me (username: echomusic)
Hotscripts Softpedia
Reply With Quote