Why does this regular expression result in: “preg_match() … No ending delimiter '^' ”? [duplicate]

半世苍凉 提交于 2019-12-25 04:05:47

问题


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];

    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/11334591/why-does-this-regular-expression-result-in-preg-match-no-ending-delimite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!