Laravel preg_match():找不到结尾分隔符'/'

Arl*_*ind 27 php regex laravel laravel-4 regexp-replace

我正在使用Laravel 4.2.我试图使用Validator验证名称字段与正则表达式,这是我的规则如下:

 public static $rules_save = [

        'class_subjects' => 'required|regex:/[0-9]([0-9]|-(?!-))+/'
    ];
Run Code Online (Sandbox Code Playgroud)

但是只要我调用规则验证就会抛出错误,请参阅下面的内容:

preg_match(): No ending delimiter '/' found
Run Code Online (Sandbox Code Playgroud)

Jos*_*ber 81

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]|-(?!-))+/'],
];
Run Code Online (Sandbox Code Playgroud)

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.