PHP:preg_match - “分隔符不能是字母数字或反斜杠”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9602321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:14:32  来源:igfitidea点击:

PHP: preg_match - "Delimiter must not be alphanumeric or backslash"

phpregex

提问by Stan

Does anyone know what is wrong with this regex? It works fine on sites like RegexPal and RegExr, but in PHP it gives me this warning and no results:

有谁知道这个正则表达式有什么问题?它在 RegexPal 和 RegExr 等网站上运行良好,但在 PHP 中它给了我这个警告但没有结果:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

Here's my code:

这是我的代码:

preg_match('name="dsh" id="dsh" value="(.*?)"', 'name="dsh" id="dsh" value="123"', $matches);

回答by Michael Berkowski

You have no delimiter. Enclose the pattern in /

您没有分隔符。将图案放入/

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);

For patterns that include /on their own, it is advisable to use a different delimiter like ~or #to avoid escaping:

对于单独包含的模式,/建议使用不同的分隔符,例如~#避免转义:

// Delimited with # instead of /
preg_match('#name="dsh" id="dsh" value="(.*?)"#', 'name="dsh" id="dsh" value="123"', $matches);

回答by Paul

You need delimiters:

您需要分隔符

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);