Laravel preg_match(): 未找到结束分隔符“/”

Laravel preg_match(): No ending delimiter '/' found

我正在研究 Laravel 4.2。我正在尝试使用 Validator 通过正则表达式验证名称字段,这是我的规则如下:

 public static $rules_save = [

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

但是一旦我调用要验证的规则就会抛出错误,请参见下文:

preg_match(): No ending delimiter '/' found

由于您的正则表达式中有管道,因此您必须使用数组:

public static $rules_save = [
    'class_subjects' => ['required', 'regex:/[0-9]([0-9]|-(?!-))+/'],
];

来自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.