0

I am writing a regular expression to extract a pattern of numbers from a string. When I used to run the below code snippet, it is showing a warning

"preg_match() [<a href='function.preg-match'>function.preg-match</a>]: No ending delimiter '^' "

Could any one tell me why this warning and how to fix it?

$temp=0;
$exp=(explode(" ",$message1));
while($temp<sizeof($exp))
{
    if(preg_match("^(+91|0091|0)?[7-9][0-9]{9}$",$exp[$temp]))
    {
    $pat=$exp[$temp];

    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Javad Shareef
  • 496
  • 7
  • 18
  • 4
    Add `/` to the beginning and end of your expression, `/^(+91|0091|0)?[7-9][0-9]{9}$/`. – Josh Jul 04 '12 at 19:36
  • You're missing the [delimiters](http://www.php.net/manual/en/regexp.reference.delimiters.php), as others have said. – Brad Koch Jul 04 '12 at 19:39
  • Now its showing compilation error as " preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 2 " – Javad Shareef Jul 04 '12 at 19:54
  • @javad_shareef: Please do not edit this question for adding an answer. Put your answer on the duplicate question. Also please do not remove the link to the duplicate question. You destroyed it with your edit. – hakre Nov 16 '12 at 08:47

1 Answers1

4

You forgot the regex delimiter, use this instead (inserted / at the very beginning and end of the regex):

if(preg_match('/^(+91|0091|0)?[7-9][0-9]{9}$/', $exp[$temp]))

The reason for the error you got is that PHP allows any delimiter character. In your case it used ^ since that's the first character in your string. However, that obviously didn't work since it never found another caret to end the regex. Using ^ would be a bad idea anyway since it has a meaning in the regex itself.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636