An example of regular expressions
POSIX Regular expression Functions
Using ereg() to Match Patterns in Strings
Searching string "aaron academy" for the letters "aa".
ereg("aa","aaron academy",$array)
2
aa
Using a single dot (.) to match any character:
ereg("d.","aardvark advocacy",$array)
2
dv
Using Quantifiers to Match a Character More Than Once
Pattern a+ will match at least one "a" followed by "a" zero or more times.
ereg("a+","aaaa", $array)
aaaa
More advanced example:
We are looking for a code that can be between one and four instances of the letter "y" followed by any number of alphanumeric characters, followed by the number 99.
$test = the code is yyXGDH99 -- have you received my sub?
ereg( "y{1,4}.*99 ", $test, $array )
Found: yyXGDH99
If you use the string: my code is yyXGDH99 did you get my 1999 sub? the result of ereg( "y{1,4}.*99 ", $test, $array ) will be:
Found: y code is yyXGDH99 did you get my 1999
As you see regular expressions match as many characters as they can.
Matching Ranges of Characters with Character Classes, eg [ab]
$test = my code is yyXGDH99 did you get my 1999 sub?
ereg( "y{1,4}[a-zA-Z0-9]*99 ", $test, $array ) )
Found membership: yyXGDH99
Working with Atoms - An atom is a pattern enclosed in brackets
$test = abbaxabbaxabbax
ereg( "([ab]+x){2}", $test, $array )
abbaxabbax
You can combine patterns with the pipe (|) character to create branches in your regular expressions.
$test = www.somedomain.com
ereg( "\.com|\.co\.uk", $test, $array )
it is a .com domainUsing egrep_replace() to find a pattern in string and repleace it
egrep_replace() enables you to find a pattern in a string and replace it with a new substring.
$test = Our Secretary, Sarah Williams is pleased to welcome you.
ereg_replace("Sarah Williams", "Rev. P.W. Goodchild", $test)
Our Secretary, Rev. P.W. Goodchild is pleased to welcome you.Using split() to break up strings
Using split() to Break Up Strings
$text = apples, oranges, peaches and grapefruit
$fruitarray = split( ", | and ", $text )
applesorangespeachesgrapefruitPerl Compatible Regular Expressions (PCREs)
Matching strings with preg_match()
Using preg_match() function to match strings.
$text = pepperoni spot;
preg_match( "/p.t/", $text, $array );
$array[0] = pot
Regular expressions will attempt to match as many characters as possible.
$text = some pepperoni post pat spot sport;
preg_match( "/p.*t/", $text, $array );
$array[0] = pepperoni post pat spot sport
So "p.*t" means p followed by as many characters as possible followed by t.
But "p.*?t" means p followed by as few characters as possible followed by t.
$text = some pepperoni post pat spot sport;
preg_match( "/p.*?t/", $text, $array );
$array[0] = pepperoni post
Matching all instances with preg_match_all()
It is sometimes difficult with with POSIX regular expressions to match every instance of a pattern within a string.
For example we use ereg() function:
$text = There are a lot of spots, pots, pilots, plants, peperonis, pistachios, Picassos, pianos, pudles and parrots;
ereg( "(^|[^a-zA-Z0-9_])(p[a-zA-Z0-9_]+s)([^a-zA-Z0-9_]|$)",$text, $array );
$array[0]: pots,
$array[1]:
$array[2]: pots
$array[3]: ,
The same with preg_match_all():
preg_match_all( "/\bp\w+s\b/", $text, $array );
$array[0][0]: pots
$array[0][1]: pilots
$array[0][2]: plants
$array[0][3]: peperonis
$array[0][4]: pistachios
$array[0][5]: pianos
$array[0][6]: pudles
$array[0][7]: parrots
We can do the same with dates:
$text = 02-02-89, 04-12-92, 12-05-04;preg_match_all( "/(\d+)-(\d+)-(\d+)/", $text, $array );
$array[0][0]: 02-02-89
$array[0][1]: 04-12-92
$array[0][2]: 12-05-04
$array[1][0]: 02
$array[1][1]: 04
$array[1][2]: 12
$array[2][0]: 02
$array[2][1]: 12
$array[2][2]: 05
$array[3][0]: 89
$array[3][1]: 92
$array[3][2]: 04
Replacing Patterns with preg_replace()
Example of changing a month with day in the date.
$dates = 27/03/97, 19/2/03;
preg_replace( "|\b(\d+)/(\d+)/(\d+)\b|", "\\2/\\1/\\3", $dates );
$dates = 03/27/97, 2/19/03
You can use arrays for regular expressions and replacements:
$text = 27/03/97, 19/2/03. It was done in 1989;
$regulars = array( "|\b(\d+)/(\d+)/(\d+)\b|", "/([Ii]t was done in) 1989/" );
$replacements = array( "\\2/\\1/\3", "\\1 2004" );
$text = preg_replace( $regulars, $replacements, $text );
$text = 03/27/97, 2/19/03. It was done in 2004
Back to main page