Perl Regular Expressions cheat sheet

/.*/ : matches anything
/.*oose/ : wild card .*

[] : class
[a-z] : any lower case letter
[A-Z] : any upper case letter
[a-zA-Z] : any upper or lower case letter, notice runs together
[a-zA-Z0-9] : any upper/lower or zero to nine
[ a-z] : any lower case letter OR a space!
[\-] : any hyphen, notice the excerpting from special inner expression functionality \
[^a] : negated class, match if sentence does NOT have ‘a’

\d = [0-9]
\D = [^0-9]
\w = [a-zA-Z0-9_]
\s = [ \t\n\r\f]
\S = [^ \t\n\r\f]

In search string NOT classes additional things:
^ at beginning means it must start to match
$ at the end means it must end to match
\b then string means must be beginning of word
string then \b means it must be end of word
\B before or after means it must NOT be start or end

my $example = “This is still a very strange sentence”;
if($example =~ m/\bstill\b/){ print “This string HAS the exact word ‘still’ in it somewhere at least once, not a variation thereof such as stillness”; }

example 2:
my $phone = (000) 000-0000;
if($phone =~ /^\(\d\d\d\) \d\d\d\d$/) { print “phone number formatted correctly”; }

Comments are closed.