Tedd's REGEXform
Regular Expression |
Will match... |
/foo/ |
The string "foo" |
/^foo/ |
'foo' at the start of a string |
/foo$/ |
'foo' at the end of a string |
/^foo$/ |
'foo' when it is alone on a string |
/[abc]/ |
a, b, or c |
/[a-z]/ |
Any lowercase letter |
/[^A-Z]/ |
Any character that is not a uppercase letter |
/(gif|jpeg)/ |
Matches either 'gif' or 'jpeg' |
/[a-z]+/ |
One or more lowercase letters |
/[0-9\.\-]/ |
Аny number, dot, or minus sign |
/^[a-zA-Z0-9_]{1,}$/ |
Any word of at least one letter, number or _ |
/([wx])([yz])/ |
wy, wz, xy, or xz |
/[^A-Za-z0-9]/ |
Any symbol (not a number or a letter) |
/([A-Z]{3}|[0-9]{4})/ |
Three letters or four numbers |
/ab*/ |
An 'a' followed by ZERO or more 'b's |
/ab+/ |
An 'a' followed by ONE or more 'b's |
/ab?/ |
An 'a' followed by zero OR one 'b' |
/ab{2}/ |
An 'a' followed by exactly two b's |
/ab{2,}/ |
An 'a' followed by two or more b's |
/a(bc)/ |
An 'a' followed by zero or one copy of 'bc' |
/a(bc)*/ |
An 'a' followed by zero or more copies of 'bc' |
/a(bc){1,5}/ |
An 'a' followed by one through five copies of 'bc' |
/a(bc){*}/ |
An 'a' followed by zero or more copies of 'bc' |
/a.[0-9]/ |
An 'a' followed by a digit |
/[a-d]/ |
An 'a' followed by lowercase letters 'a' through 'd' |
/(a|b)*c/ |
Has a sequence of a's or b's ending in a c |
/(b|cd)ef/ |
Has either 'aef' or 'cdef' in it |
/^.{3}$/ |
Has exactly 3 characters. |
/$|¥[0-9]/ |
Has a $ or ¥ followed by 0-9 |
/^[a-zA-Z]/ |
Starts with any letter. |
/[0-9]%/ |
Single digit before a percent sign. |
/,[a-zA-Z0-9]$/ |
Ends in a comma followed by an alphanumeric character. |
/%[^a-zA-Z]%/ |
Single character that is NOT a letter between two percent signs. |
/[\s,]+/ |
Same as explode on ' ' via preg_split() |
/.at/ |
Three-character string ending with 'at', including 'hat', 'cat', and 'bat'. |
/[^b]at/ |
Same as above except 'bat'. |
/[^bc]at/ |
Same as above except 'bat' and 'cat'. |
/^[bc]at/ |
Allows 'bat' and 'cat', but only at the start of the string. |
/[bc]at/$ |
Allows 'bat' and 'cat', but only at the end of the string. |
/\[.\]/ |
Any single character surrounded by '[' and ']', such as [a], [b]. |
/a.+c/ |
Greedy : abcdefghijklmc will return 'abcdefghijklmc'. |
/a.*c/ |
Greedy : abcdefghijklmc will return 'abcdefghijklmc'. |
/a.+?c/ |
Lazy : abcdefghijklmc will return 'abc'. |
/a.*?c/ |
Lazy : abcdefghijklmc will return 'abc'. |
/a.+c/U
Lazy : abcdefghijklmc will return 'abc'. |
|
/a.*c/U |
Lazy : abcdefghijklmc will return 'abc'. |
The standard quantifiers in Regular Expressions are GREEDY, meaning they match as much as they can,
only giving back as necessary to match the remainder of the regex. By using a LAZY quantifier, the
expression tries the minimal match first.
The LAZY qualifier can be expressed as a "?" inside the pattern OR "U" at the end of the pattern, such
as /a.*?c/ and /a.*c/U that produce the same result.
CODE FOLLOWS
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
$self = basename($_SERVER['SCRIPT_NAME']);
echo('<h1>Tedd\'s REGEXform</h1>');
$matches[0] = '';
$split[0] = '';
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$pattern = isset($_POST['pattern']) ? $_POST['pattern'] : "/ell/";
$string = isset($_POST['string']) ? $_POST['string'] : "Hello World";
$i = 1;
if($step == 0) // first time into this form
{
?>
<form action="<?php echo($self); ?>" method="post" >
<fieldset>
<legend>REGEX:</legend>
<label for="pattern">Pattern: </label>
<input type="text" size="30" id="pattern" name="pattern" value="<?php echo($pattern); ?>">
<br>
<label for="string">String: </label>
<input type="text" size="30" id="string" name="string" value="<?php echo($string); ?>">
</fieldset>
<p>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</p>
</form>
<table class="full">
<tr>
<th>Regular Expression</th>
<th>Will match...</th>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/foo/</td>
<td>The string "foo"</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/^foo/</td>
<td>'foo' at the start of a string</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/foo$/</td>
<td>'foo' at the end of a string</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/^foo$/</td>
<td>'foo' when it is alone on a string</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[abc]/</td>
<td>a, b, or c</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[a-z]/</td>
<td>Any lowercase letter</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[^A-Z]/</td>
<td>Any character that is not a uppercase letter</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/(gif|jpeg)/</td>
<td>Matches either 'gif' or 'jpeg'</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[a-z]+/</td>
<td>One or more lowercase letters</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[0-9\.\-]/</td>
<td>Аny number, dot, or minus sign</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/^[a-zA-Z0-9_]{1,}$/</td>
<td>Any word of at least one letter, number or _</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/([wx])([yz])/</td>
<td>wy, wz, xy, or xz</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[^A-Za-z0-9]/</td>
<td>Any symbol (not a number or a letter)</td></tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/([A-Z]{3}|[0-9]{4})/</td>
<td>Three letters or four numbers</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/ab*/</td>
<td >An 'a' followed by ZERO or more 'b's</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/ab+/ </td>
<td>An 'a' followed by ONE or more 'b's</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/ab?/</td>
<td> An 'a' followed by zero OR one 'b'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/ab{2}/ </td>
<td>An 'a' followed by exactly two b's</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/ab{2,}/</td>
<td>An 'a' followed by two or more b's</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a(bc)/ </td>
<td> An 'a' followed by zero or one copy of 'bc'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a(bc)*/</td>
<td> An 'a' followed by zero or more copies of 'bc'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a(bc){1,5}/</td>
<td> An 'a' followed by one through five copies of 'bc'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a(bc){*}/ </td>
<td> An 'a' followed by zero or more copies of 'bc'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.[0-9]/ </td>
<td> An 'a' followed by a digit</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[a-d]/ </td>
<td> An 'a' followed by lowercase letters 'a' through 'd'</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/(a|b)*c/</td>
<td>Has a sequence of a's or b's ending in a c</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/(b|cd)ef/</td>
<td> Has either 'aef' or 'cdef' in it</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/^.{3}$/ </td>
<td> Has exactly 3 characters.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/$|¥[0-9]/</td>
<td> Has a $ or ¥ followed by 0-9</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/^[a-zA-Z]/</td>
<td>Starts with any letter.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[0-9]%/ </td>
<td>Single digit before a percent sign.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/,[a-zA-Z0-9]$/</td>
<td>Ends in a comma followed by an alphanumeric character.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/%[^a-zA-Z]%/ </td>
<td>Single character that is NOT a letter between two percent signs.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[\s,]+/ </td>
<td>Same as explode on ' ' via preg_split()</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/.at/ </td>
<td>Three-character string ending with 'at', including 'hat', 'cat', and 'bat'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[^b]at/ </td>
<td>Same as above except 'bat'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[^bc]at/ </td>
<td>Same as above except 'bat' and 'cat'. </td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td> /^[bc]at/ </td>
<td>Allows 'bat' and 'cat', but only at the start of the string.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/[bc]at/$</td>
<td>Allows 'bat' and 'cat', but only at the end of the string.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/\[.\]/ </td>
<td>Any single character surrounded by '[' and ']', such as [a], [b].</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.+c/ </td>
<td>Greedy : abcdefghijklmc will return 'abcdefghijklmc'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.*c/ </td>
<td>Greedy : abcdefghijklmc will return 'abcdefghijklmc'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.+?c/ </td>
<td>Lazy : abcdefghijklmc will return 'abc'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.*?c/ </td>
<td>Lazy : abcdefghijklmc will return 'abc'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.+c/U<t/d>
<td>Lazy : abcdefghijklmc will return 'abc'.</td>
</tr>
<tr class="row<?php echo($i++ & 1 );?>">
<td>/a.*c/U</td>
<td>Lazy : abcdefghijklmc will return 'abc'.</td>
</tr>
</table>
<p>
The standard quantifiers in Regular Expressions are GREEDY, meaning they match as much as they can,
only giving back as necessary to match the remainder of the regex. By using a LAZY quantifier, the
expression tries the minimal match first.
</p>
<p>
The LAZY qualifier can be expressed as a "?" inside the pattern OR "U" at the end of the pattern, such
as /a.*?c/ and /a.*c/U that produce the same result.
</p>
<?php
}
else
{
$a = array();
echo("<p >Pattern: $pattern<br>String: $string</p>");
// preg_match(string pattern, string string, array matches)
echo ('<p class="bold">preg_match()</p>');
if (preg_match($pattern, $string, $matches))
{
echo('<pre>');
print_r( $matches);
echo('</pre>');
}
else
{
echo('<pre>');
print_r( $a);
echo('</pre>');
}
// preg_match_all(string pattern, string string, array matches)
echo ('<p class="bold">preg_match_all()</p>');
if (preg_match_all($pattern, $string, $matches))
{
echo('<pre>');
print_r( $matches);
echo('</pre>');
}
else
{
echo('<pre>');
print_r( $a);
echo('</pre>');
}
// preg_split ( string pattern, string subject [, int limit [, int flags]])
$split = preg_split($pattern, $string, PREG_SPLIT_OFFSET_CAPTURE);
echo ('<p class="bold">preg_split() </p>');
echo('<pre>');
print_r( $split);
echo('</pre>');
// provide a form to try again
?>
<form action="<?php echo($self); ?>" method="post" >
<p>
<input type="hidden" name="pattern" value="<?php echo($pattern);?>">
<input type="hidden" name="string" value="<?php echo($string);?>">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="try Again">
</p>
</form>
<?php
}
include('includes/footer.php');
?>