
I am learning how to select parts of text in php with more advanced regular expressions.
preg_match
preg_match is a php function that checks regular expression. The first argument passed is a regular expression and the second is the string to which the check is made. With the two argument the preg_match will return a true or false.
Character Sets
Character sets are denoted by brackets [ ] and it matches any of the characters within the brackets. You can combine with one of the repetition '+' '*' operators. You can also match a range (seen in example 3) and you can match on what is negated (example 4)
$reg_exp = '/a[123]b/';
$string = 'a2b';
echo 'this matches: ' . preg_match($reg_exp, $string);
echo '<br/>';
//example 2
$reg_exp = '/a[123]+b/';
$string = 'a222331b';
echo 'this also matches: ' . preg_match($reg_exp, $string);
echo '<br/>';
//example 3
$reg_exp = '/a[1-3]+b/';
$string = 'a222341b';
echo 'this does not match: ' . preg_match($reg_exp, $string);
echo '<br/>';
//example 4
$reg_exp = '/a[^123]+b/';
$string = 'a45666b';
echo 'this will match: ' . preg_match($reg_exp, $string);
echo '<br/>';
Capture Groups
How to extract parts of a string with regular expressions. In preg_match there is an optional third parameter which is an array and that contains the result of the match. In Example 1 code below it will produce the below display:
this matches: 1
$matches: array(1) { [0]=> string(6) "a2222b" }
You can capture regular expressions inside the parentheses to create sub patterns. Example 2 will print:
this matches: 1
$matches: array(2) { [0]=> string(6) "a2222b" [1]=> string(4) "2222" }
Example 3 a more complex example:
this matches: 1
$matches: array(3) { [0]=> string(8) "Jan 1992" [1]=> string(3) "Jan" [2]=> string(4) "1992" }
//example 1
$reg_exp = '/a[123]+b/';
$string = 'a2222b';
echo 'this matches: ' . preg_match($reg_exp, $string, $matches);
echo '<br/>';
echo '$matches: ';
echo '<br/>';
var_dump($matches);
echo '<br/>';
//example 2
$reg_exp = '/a([123]+)b/';
$string = 'a2222b';
echo 'this matches: ' . preg_match($reg_exp, $string, $matches);
echo '<br/>';
echo '$matches: ';
echo '<br/>';
var_dump($matches);
echo '<br/>';
//example 3
$reg_exp = '/([a-zA-Z]+) (\d+)/';
$string = 'Jan 1992';
echo 'this matches: ' . preg_match($reg_exp, $string, $matches);
echo '<br/>';
echo '$matches: ';
echo '<br/>';
var_dump($matches);
echo '<br/>';
Named Capture Groups
You can add named capture groups so the key of the $matches array can be named as to your liking. In Example 1 is the same as previous example but named. It produced:
this matches: 1
$matches:
array(5) { [0]=> string(8) "Jan 1992" ["month"]=> string(3) "Jan" [1]=> string(3) "Jan" ["year"]=> string(4) "1992" [2]=> string(4) "1992" }
//example 1
$reg_exp = '/(?<month>[a-zA-Z]+) (?<year>\d+)/';
$string = 'Jan 1992';
echo 'this matches: ' . preg_match($reg_exp, $string, $matches);
echo '<br/>';
echo '$matches: ';
echo '<br/>';
var_dump($matches);
echo '<br/>';