gpt4 book ai didi

php - PCRE2 正则表达式错误转义序列在字符类中无效

转载 作者:行者123 更新时间:2023-12-04 15:16:29 25 4
gpt4 key购买 nike

我有以下正则表达式,无论出于何种原因,在将其与 PCRE2 一起使用时我总是遇到错误。我不确定是什么导致了错误。

/^.(?=.{1,})(?=.[A-Z])(?=.[0-9])(?=.[\d\X])(?=(?:.[!@#$%^&()\\\-_=\+{}[\]|;:,.]){1,}).{8,}$/

日志中的错误是:

异常:preg_match():编译失败:转义序列在字符类中偏移量 43 处无效

最佳答案

根据这个Red Hat Bugzilla bug , 这是一个 documented PCRE2 behavior :

Escape sequences in character classes

All the sequences that define asingle character value can be used both inside and outside characterclasses. In addition, inside a character class, \b is interpreted asthe backspace character (hex 08).

When not followed by an opening brace, \N is not allowed in acharacter class. \B, \R, and \X are not special inside a characterclass. Like other unrecognized alphabetic escape sequences, they causean error. Outside a character class, these sequences have differentmeanings.

为了修复你的正则表达式,我建议像这样

if (preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&()\\\\_=+{}[\]|;:,.-]).{8,}$/', 'aB9!ssssddssdd')){
echo "yes";
}

在哪里

  • ^ - 字符串的开始
  • (?=.*[A-Z]) - 至少一个大写 ASCII 字母
  • (?=.*[a-z]) - 至少一个小写 ASCII 字母
  • (?=.*[0-9]) - 至少一个 ASCII 数字
  • (?=.*[!@#$%^&()\\\\_=+{}[\]|;:,.-]) - 至少一个特殊的char, !, @, #, $, %, ^, &, (, ), \, _ , =, +, {, }, [, ] , |, ;, :, ,, .-
  • .{8,} - 至少 8 个字符,无换行符
  • $ - 字符串结尾。

关于php - PCRE2 正则表达式错误转义序列在字符类中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64248370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com