php Laravel preg_match():找不到结束分隔符“/”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32810385/
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
Laravel preg_match(): No ending delimiter '/' found
提问by Arlind
Im working on Laravel 4.2. Im trying to use Validator to validate a name field with regex, here is my rule below:
我正在研究 Laravel 4.2。我试图使用 Validator 用正则表达式验证名称字段,这是我的规则如下:
public static $rules_save = [
'class_subjects' => 'required|regex:/[0-9]([0-9]|-(?!-))+/'
];
But as soon as I call the rule to be validated an error is thrown, see below:
但是一旦我调用要验证的规则,就会抛出错误,请参见下文:
preg_match(): No ending delimiter '/' found
回答by Joseph Silber
Since your regex has a pipe in it, you have to use an array:
由于您的正则表达式中有一个管道,因此您必须使用一个数组:
public static $rules_save = [
'class_subjects' => ['required', 'regex:/[0-9]([0-9]|-(?!-))+/'],
];
From the docs:
从文档:
When using the
regex
pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
使用
regex
模式时,可能需要在数组中指定规则而不是使用管道分隔符,尤其是当正则表达式包含管道字符时。