Laravel - 断言多个正则表达式
Laravel - Assertion multiple regex
我有两个正则表达式:
'/^(?:0(?:21|9[0-9]))?[0-9]{8}$/'
和
'/(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}/'
我想在Laravel中使用Assertion::regex方法。这是该方法:
Assertion.php:
public static function regex($value, $pattern, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
if (! preg_match($pattern, $value)) {
$message = sprintf(
$message ?: 'Value "%s" does not match expression.',
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, array('pattern' => $pattern));
}
return true;
}
如何在 Assertion::regex($phone, $regex); ?
中使用和检查多个正则表达式
我曾经用 :
初始化 $regex
$regex = '/^(?:0(?:21|9[0-9]))?[0-9]{8}$/ | /(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}/'
其实我报错了:
preg_match(): Unknown modifier '|'
有什么建议吗?
如果您想在两个正则表达式之间交替使用,管道必须在正则表达式内:
^(?:0(?:21|9[0-9]))?[0-9]{8}$|(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}
^
您所做的字符串连接导致 php 理解您的正则表达式在此反斜杠后完成:
/^(?:0(?:21|9[0-9]))?[0-9]{8}$/
^
试图将后面的内容解释为修饰符,因此您收到了错误消息。
我有两个正则表达式:
'/^(?:0(?:21|9[0-9]))?[0-9]{8}$/'
和
'/(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}/'
我想在Laravel中使用Assertion::regex方法。这是该方法:
Assertion.php:
public static function regex($value, $pattern, $message = null, $propertyPath = null)
{
static::string($value, $message, $propertyPath);
if (! preg_match($pattern, $value)) {
$message = sprintf(
$message ?: 'Value "%s" does not match expression.',
static::stringify($value)
);
throw static::createException($value, $message, static::INVALID_REGEX, $propertyPath, array('pattern' => $pattern));
}
return true;
}
如何在 Assertion::regex($phone, $regex); ?
中使用和检查多个正则表达式我曾经用 :
初始化 $regex$regex = '/^(?:0(?:21|9[0-9]))?[0-9]{8}$/ | /(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}/'
其实我报错了:
preg_match(): Unknown modifier '|'
有什么建议吗?
如果您想在两个正则表达式之间交替使用,管道必须在正则表达式内:
^(?:0(?:21|9[0-9]))?[0-9]{8}$|(0|\+98 | 98)?([ ]|,|-|[()]){0,2}9[1|2|3|4]([ ]|,|-|[()]){0,2}(?:[0-9]([ ]|,|-|[()]){0,2}){8}
^
您所做的字符串连接导致 php 理解您的正则表达式在此反斜杠后完成:
/^(?:0(?:21|9[0-9]))?[0-9]{8}$/
^
试图将后面的内容解释为修饰符,因此您收到了错误消息。