Simple PHP regular expression question

tincoatr

New Member
What's the most efficient preg_match regular expression for the following:[*]The regular expression must match a certain string (case insensitive)[*]Can be followed by [ or ; and then something elseThese are the test cases for "foo":
  • foo --> good
  • food --> bad
  • foo; --> bad, need something after ;
  • FOO;bar --> good
  • foo[bar] --> good
  • foo[ --> bad, need something after ]
  • fOo[bar]1;2;3 --> good
This is my test code:\[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;}?>\[/code\]
 
Back
Top