Basic form of regex:


$string =~ /text_to_search_for/     #Returns true if text_to_search_for is found

$string !~ /text_to_search_for/     #Returns true if text_to_search_for is NOT found

The // can be replaced with any other character by using the m option
$String =~ m!hello!                 #Returns true hello
is found, the // has been replaced by !!



^ String begins with

example: $string =~ /^first_text/

#True if $string begins with first_text


$ String ends with

example: $string =~ /last_text$/

#True if $string ends with last_text


\n newline character

\t tab character

\. period character

\< left arrow character

\s white space character (space or tab)

\w "word"  character (letter, digit, or underscore)

\d "digit" character (0-9)

\S NOT a white space character (space or tab)

\W NOT a "word"  character (letter, digit, or underscore)

\D NOT a "digit" character (0-9)


. any character

example: $string =~ /f.t/

#True if $string contains fit, fat fyt, f t


+ the preceding character 1 or more times 

example: $string =~ /f+t/    

#True if $string has ft, fft fffft, but not fxt

*the preceding character 0 or more times 

example: $string =~ /f*t/    

#True if $string contains ft, fft fffft, or xxxt

? the preceding character 0 or 1 times    

example: $string =~ /f?t/    

#True if $string contains ft, t, xt, but not fft


Fixed number of repeats:

$phone =~ /\d\d\d-\d\d\d\d/ 

is the same as

$phone =~ /\d{3}-\d{4}/

\d{2,5} will match two to five digits

\w{3,} will match a word that's at least three characters long.

Multiple conditions:

example: $string =~ /^http:.+html$/   

#True if $string starts with http:

                       followed by one or more characters

                       and ends with html


Match any in a set:

$string =~ /[aeiou]/      #True if any vowel (aeiou) is found

$string =~ /[a,d]/            #True if any letter a to d is found

$string =~ /[^aeiou]/         #True if NO vowel (aeiou) is found



Upper or lower case:

$string =~ /bob/              #True if bob is found,

but not BOB or Bob or boB

$string =~ /bob/i             #True if bob or BOB or Bob or boB


Search for more than 1 item:

$string =~ /idiot|dope|twit/  #True if idiot or dope or twit is found


Returned values:

if ($string =~ /b.b/) {$1 contains the matching text ie bob bib ...

 if any match found}

if ($string =~ /(b.b).+(w.w)/) {$1 contains the matching text ie bob

   bib ... $2= wow or wlw...

                  if $string holds bob anything wow}


Search and replace:

$string =~ s/search_text/replace_text/

# if search_text is found in $string

replace it with replace_text