且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 PCRE 转换为 POSIX 正则表达式

更新时间:2023-02-17 22:31:24

MySQL 文档 声明:

MySQL 使用 Henry Spencer 的正则表达式实现,旨在符合 POSIX 1003.2.MySQL 使用扩展版本来支持在 SQL 语句中使用 REGEXP 运算符执行的模式匹配操作.

MySQL uses Henry Spencer's implementation of regular expressions, which is aimed at conformance with POSIX 1003.2. MySQL uses the extended version to support pattern-matching operations performed with the REGEXP operator in SQL statements.

好的,所以我们谈论的是 POSIX ERE.

Ok, so we're talking about POSIX ERE.

本页列出了各种正则表达式之间的详细信息,所以我会使用它作为备忘单.

This page lists the details between various regex flavors, so I'll use it as a cheatsheet.

  1. ^[A-z\.]{3,36}

您正在使用:

  • 锚点:^
  • 字符类:[...]
  • 范围量词:{n,m}

所有这些都在 POSIX ERE 中开箱即用,因此您可以按原样使用此表达式.但是转义字符类中的 . 是多余的,而 Az 在字符类中很可能是错误的(它包括 [\]^_\`),所以只写:

All of these are supported out of the box in POSIX ERE, so you can use this expression as-is. But escaping the . in the character class is redundant, and A-z is most probably wrong in a character class (it includes [\]^_\`), so just write:

^[A-Za-z. ]{3,36}

  • ^[a-z\d\.]{3,24}$

    这个也使用 \d ,它在 POSIX ERE 中不受支持.所以你必须写:

    This one uses \d as well, which is unsupported in POSIX ERE. So you have to write:

    ^[a-z0-9.]{3,24}$
    

  • ^(?=^.{4,}$)(?=.*\d)(?=.*[az])(?=.*[AZ])(?!.*\s).*$

    嗯.您正在使用前瞻.这些完全超出了 POSIX ERE 的范围,但您可以通过组合多个 SQL 子句以获得等效逻辑来解决此限制:

    Meh. You're using lookaheads. These are totally out of the scope for POSIX ERE, but you can work around this limitation by combining several SQL clauses for an equivalent logic:

    WHERE LENGTH(foo) >= 4
      AND foo REGEXP '[0-9]'
      AND foo REGEXP '[a-z]'
      AND foo REGEXP '[A-Z]'
      AND NOT foo REGEXP '[ \t\r\n]'