【问题标题】:regex:pattern validation in Laravel [duplicate]正则表达式:Laravel 中的模式验证 [重复]
【发布时间】:2019-06-28 05:57:14
【问题描述】:

我尝试在 Laravel 中验证 facebook.com 网址。 这是我的正则表达式:

    'facebook' => 'url|regex:((?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*))|nullable',

它在在线正则表达式测试网站 (https://regex101.com/) 中工作,但在 Laravel 中。我有错误

“preg_match(): 没有找到结束匹配分隔符')'”。

有什么问题?

【问题讨论】:

  • 似乎。你应该写regex:/.../
  • 我试了一下,结果:“preg_match(): No end delimiter '/' found”
  • 'facebook' => 'url|regex:/((?:(?:http|https):\/\/)?(?:www.)?facebook.com\/( ?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(? =\d.*))?([\w\-]*))/|可为空的'

标签: php regex laravel validation


【解决方案1】:

在官方documentation看到这个说明:

当使用 regex / not_regex 模式时,可能需要 在数组中指定规则而不是使用管道分隔符,尤其是 如果正则表达式包含管道字符。

所以,这应该可行:

'facebook' => ['url', 'regex:/(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*))/', 'nullable']

【讨论】:

  • error "preg_match(): Compilation failed: assertion expected after (?( or (?(?C) at offset 51"
  • 您的模式中似乎有一个额外的(。现在可以试试吗?
猜你喜欢
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 2017-03-03
相关资源
最近更新 更多