REGEX

.

$string1 = "Hello World\n"; if ($string1 =~ m/…../) { print "$string1 has length >= 5\n"; }

( )

$string1 = "Hello World\n"; if ($string1 =~ m/(H..).(o..)/) { print "We matched ‘$1’ and ‘$2’\n"; }

Output:

We matched ‘Hel’ and ‘o W’;

+

$string1 = "Hello World\n"; if ($string1 =~ m/l+/) { print "There are one or more consecutive letter \"l\"’s in $string1\n"; }

Output:

There are one or more consecutive letter "l"’s in Hello World

?

$string1 = "Hello World\n"; if ($string1 =~ m/H.?e/) { print "There is an ‘H’ and a ‘e’ separated by "; print "0-1 characters (Ex: He Hoe)\n"; }

?

$string1 = "Hello World\n"; if ($string1 =~ m/(l.+?o)/) { print "The non-greedy match with ‘l’ followed by one or "; print "more characters is ‘llo’ rather than ‘llo wo’.\n"; }

*

$string1 = "Hello World\n"; if ($string1 =~ m/el*o/) { print "There is an ‘e’ followed by zero to many "; print "’l’ followed by ‘o’ (eo, elo, ello, elllo)\n"; }

{M,N}

$string1 = "Hello World\n"; if ($string1 =~ m/l{1,2}/) { print "There exists a substring with at least 1 "; print "and at most 2 l’s in $string1\n"; }

[…]

$string1 = "Hello World\n"; if ($string1 =~ m/[aeiou]+/) { print "$string1 contains one or more vowels.\n"; }

|

$string1 = "Hello World\n"; if ($string1 =~ m/(Hello|Hi|Pogo)/) { print "At least one of Hello, Hi, or Pogo is "; print "contained in $string1.\n"; }

\b

$string1 = "Hello World\n"; if ($string1 =~ m/llo\b/) { print "There is a word that ends with ‘llo’\n"; }

\w

$string1 = "Hello World\n"; if ($string1 =~ m/\w/) { print "There is at least one alphanumeric "; print "character in $string1 (A-Z, a-z, 0-9, _)\n"; }

\W

$string1 = "Hello World\n"; if ($string1 =~ m/\W/) { print "The space between Hello and "; print "World is not alphanumeric\n"; }

\s

$string1 = "Hello World\n"; if ($string1 =~ m/\s.*\s/) { print "There are TWO whitespace characters, which may"; print " be separated by other characters, in $string1"; }

\S

$string1 = "Hello World\n"; if ($string1 =~ m/\S.*\S/) { print "There are TWO non-whitespace characters, which"; print " may be separated by other characters, in $string1"; }

\d

$string1 = "99 bottles of beer on the wall."; if ($string1 =~ m/(\d+)/) { print "$1 is the first number in ‘$string1’\n"; }

Output:

99 is the first number in ’99 bottles of beer on the wall.’

\D

$string1 = "Hello World\n"; if ($string1 =~ m/\D/) { print "There is at least one character in $string1"; print " that is not a digit.\n"; }

^

$string1 = "Hello World\n"; if ($string1 =~ m/^He/) { print "$string1 starts with the characters ‘He’\n"; }

$

$string1 = "Hello World\n"; if ($string1 =~ m/rld$/) { print "$string1 is a line or string "; print "that ends with ‘rld’\n"; }

\A

$string1 = "Hello\nWorld\n"; if ($string1 =~ m/\AH/) { print "$string1 is a string "; print "that starts with ‘H’\n"; }

\z

$string1 = "Hello\nWorld\n"; if ($string1 =~ m/d\n\z/) { print "$string1 is a string "; print "that ends with ‘d\\n’\n"; }

[^…]

$string1 = "Hello World\n"; if ($string1 =~ m/[^abc]/) { print "$string1 contains a character other than "; print "a, b, and c\n"; }

Posted in PHP