Reputation: 196
What's the most efficient preg_match regular expression for the following:
These are the test cases for "foo":
This is my test code:
<?php
$tests = array();
$tests[] = 'foo';
$tests[] = 'food';
$tests[] = 'foo;';
$tests[] = 'FOO;bar';
$tests[] = 'foo[bar]';
$tests[] = 'foo[';
$tests[] = 'foo[]';
$tests[] = 'fOo[bar]1;2;3';
foreach ($tests as $test)
{
echo $test, ' --> ';
$found = preg_match('REGULAR EXPRESSION HERE', $test);
if ($found === false || $found < 1)
echo 'bad';
else
echo 'ok';
echo '<br>', PHP_EOL;
}
?>
Upvotes: 0
Views: 95
Reputation: 93167
You can try this regex :
/foo(;.+?|\[.+?\].*?)*$/i
If your bracket doesn't need to be closed :
/foo([;\[].+?)*$/i
If your bracket or semicolon must not be the last part of your expression :
/foo([;\[][^;\[]+)*$/i
All passed the tests with Regex planet.
Resources :
Upvotes: 2