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

标签 php regex laravel laravel-4 regexp-replace

我正在开发 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.

关于php - Laravel preg_match() : No ending delimiter '/' found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810385/

相关文章:

php - PDO多次插入记录,等于数据库值

php - Laravel 8 将所有异常返回为 json

regex - Excel VBA 正则表达式匹配位置

php - Laravel 4 中具有特定角色的用户的额外字段

migration - 在 Laravel 中为现有 InnoDB 关系设置 Eloquent 关系

php - 添加多次PHP

php - CodeIgniter:提交无效表单后如何保留值?

javascript - 正则表达式回调函数只执行一次

regex - R:如何从数据框中提取一些大数字,而不是其他数字

javascript - 在 Laravel Blade 的表单字段中使用变量