Laravel规则与问题正则表达式(OR)运算符 [英] Issue with Laravel Rules & Regex (OR) operator
问题描述
我的Laravel规则和正则表达式操作有一个小问题:
I'm having a small issue with my Laravel rules and regex operation :
基本上,规则是这样的数组:
Basically a rule is an array as such :
'room'=>'required|alpha_num|min:2|max:10',
我遇到的问题是使用正则表达式和|时. (或)运算符,例如:
The problem i'm having is when using regex and the | (or) operator such as :
'cid'=>'required|regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i',
我收到服务器错误消息:
I'm getting a server error saying :
ErrorException
preg_match(): No ending delimiter '/' found
我猜想preg_match
在/.../
内部的第一个|
处停止.
I'm guessing the preg_match
is stopping at the first |
inside the /.../
.
反正还有编写上面的代码来使其工作吗?
Is there anyway to write the above code to make it work ?
完整代码:
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
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.
要澄清: 你会做这样的事情
To clarify: You would do something like this
$rules = array('test' => array('size:5', 'regex:foo'));
这篇关于Laravel规则与问题正则表达式(OR)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!