php 分隔符不能是字母数字或反斜杠和 preg_match

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7660545/
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 03:06:56  来源:igfitidea点击:

Delimiter must not be alphanumeric or backslash and preg_match

phpregexpreg-match

提问by user547363

I have this code :

我有这个代码:

$string1 = "My name is 'Kate' and im fine"; 
$pattern = "My name is '(.*)' and im fine"; 
preg_match($pattern , $string1, $matches);
echo $matches[1];

and when im run it returns this error:

当我运行时,它返回此错误:

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

警告:preg_match() [function.preg-match]:分隔符不能是字母数字或反斜杠

回答by Paul

You need a delimiter for your pattern. It should be added at the start and end of the pattern like so:

您的模式需要一个分隔符。它应该添加在模式的开头和结尾,如下所示:

$pattern = "/My name is '(.*)' and im fine/";  // With / as a delimeter 

回答by iconoclast

The solution (which other answers don't mention—at least at the time of my originally writing this) is that when PHP refers to delimiters, it's not referring to the delimiters you see in your code (which are quote marks) but the next characters inside the string. (In fact I've never seen this stated anywhere in any documentation: you have to see it in examples.) So instead of having a regular expression syntax like what you may be accustomed to from many other languages:

解决方案(其他答案没有提到——至少在我最初写这篇文章的时候)是,当 PHP 引用分隔符时,它不是引用你在代码中看到的分隔符(它们是引号),而是下一个字符串内的字符。(事实上​​,我从未在任何文档的任何地方看到过这种说法:您必须在示例中看到它。)因此,与其使用您在许多其他语言中可能习惯的正则表达式语法:

/something/

PHP uses strings, and then looks inside the string for anotherdelimiter:

PHP 使用字符串,然后在字符串内部查找另一个分隔符:

'/something/'

The delimiter PHP is referring to is the pair of /characters, instead of the pair of 'characters. So if you write 'something', PHP will take sas the intended delimiter and complain that you're not allowed to use alphanumeric characters as your delimiter.

PHP 所指的分隔符是/字符对,而不是'字符对。因此,如果您编写'something',PHP 将s作为预期的分隔符并抱怨您不允许使用字母数字字符作为分隔符。

So if you want to pass (for instance) an ito show that you want a case-insensitve match, you pass it inside the string but outside of the regex delimiters:

因此,如果您想通过(例如) ani来表明您想要一个不区分大小写的匹配,您可以在字符串内部但在正则表达式分隔符之外传递它:

'/something/i'

If you want to use something other than /as your delimiter, you can, such as if you're matching a URL and don't want to have to escape all the slashes:

如果您想使用除/分隔符以外的其他内容,则可以,例如,如果您要匹配 URL 并且不想转义所有斜杠:

'~something~'

回答by Ben Swinburne

You must specify a delimiter for your expression. A delimiter is a special character used at the start and end of your expression to denote which part is the expression. This allows you to use modifiers and the interpreter to know which is an expression and which are modifiers. As the error message states, the delimiter cannot be a backslash because the backslash is the escape character.

您必须为表达式指定分隔符。分隔符是在表达式的开头和结尾使用的特殊字符,用于表示哪个部分是表达式。这允许您使用修饰符和解释器来知道哪些是表达式,哪些是修饰符。正如错误消息所述,分隔符不能是反斜杠,因为反斜杠是转义字符。

$pattern = "/My name is '(.*)' and im fine/";

and below the same example but with the imodifier to match without being case sensitive.

在相同的示例下方,但使用i修饰符匹配而不区分大小写。

$pattern = "/My name is '(.*)' and im fine/i";

As you can see, the iis outside of the slashes and therefore is interpreted as a modifier.

如您所见, the iis 在斜杠之外,因此被解释为修饰符。

Also bear in mind that if you use a forward slash character (/) as a delimiter you must then escape further uses of / in the regular expression, if present.

还请记住,如果您使用正斜杠字符 (/) 作为分隔符,则必须避免在正则表达式中进一步使用 /(如果存在)。

回答by Hammad Khan

The pattern must have delimiters. Delimiters can be a forward slash (/) or any non alphanumeric characters(#,$,*,...). Examples

模式必须有分隔符。分隔符可以是正斜杠 (/) 或任何非字母数字字符(#、$、*、...)。例子

$pattern = "/My name is '(.*)' and im fine/"; 
$pattern = "#My name is '(.*)' and im fine#";
$pattern = "@My name is '(.*)' and im fine@";  

回答by Danon

You can also use T-Regx librarywhich has automatic delimitersfor you:

您还可以使用具有自动分隔符的T-Regx 库

$matches = pattern("My name is '(.*)' and im fine")->match($string1)->all();

                 // ↑ No delimiters needed 

回答by Nubian

Maybe not related to original code example, but I received the error "Delimiter must not be alphanumeric or backslash" and Googled here. Reason: I mixed order of parameters for preg_match. Pattern was the second parameter and string to match was first. Be careful :)

可能与原始代码示例无关,但我收到错误“分隔符不能是字母数字或反斜杠”并在此处用 Google 搜索。原因:我混合了 preg_match 的参数顺序。模式是第二个参数,要匹配的字符串是第一个。当心 :)

回答by Vikas Naranje

Please try with this

请试试这个

 $pattern = "/My name is '\(.*\)' and im fine/";