php - Laravel 规则和正则表达式 (OR) 运算符的问题

标签 php regex laravel

我的 Laravel 规则和正则表达式操作有一个小问题:

基本上,规则就是一个数组:

'room'=>'required|alpha_num|min:2|max:10',

我遇到的问题是使用正则表达式和 | 时(或)运算符,例如:

'cid'=>'required|regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i',

我收到服务器错误:

ErrorException

preg_match(): No ending delimiter '/' found

我猜测 preg_match 停在 /.../ 内的第一个 | 处。

有没有办法编写上面的代码来使其工作?

完整代码:

public static $rules = array(

'cid' => array('required', 'regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i'),

'description'=>'required|regex:/^[A-Za-z \t]*$/i|min:3|unique:courses',

'credits'=>'required|regex:/^\d+(\.\d)?$/'

);

最佳答案

http://laravel.com/docs/validation#rule-regex

regex:pattern

The field under validation must match the given regular expression.

Note: 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.

澄清一下: 你会做这样的事情

$rules = array('test' => array('size:5', 'regex:foo'));

关于php - Laravel 规则和正则表达式 (OR) 运算符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60355745/

相关文章:

php - PHP 中的集中式错误报告

php - jquery $.get 函数的问题

php登录脚本中只有一个用户在变量中设置

正则表达式匹配以单词开头并以或不以数字结尾的字符串

mysql - Valet 和 Laravel 5.5 用户访问被拒绝 'root' @'localhost'

php - 在 codeigniter 中找不到请求的 url 错误

java - 带有正则表达式的 Java 字符串中的 SPARQL 请求

php - 需要关系数的意见

laravel - 在 Laravel 5.3 中使用 Vue 组件

regex - 使用正则表达式搜索文本