将 PCRE 转换为 POSIX 正则表达式 [英] Converting PCRE to POSIX regular expression

查看:61
本文介绍了将 PCRE 转换为 POSIX 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 MySQL 数据库,并注意到它本身不支持 PCRE(需要插件).

我希望将这三个用于一些数据验证(这些实际上是赋予 pattern 属性的值):

I wish to use these three for some data validation (these are actually the values given to the pattern attribute):

  1. ^[A-z\.]{3,36}
  2. ^[a-z\d\.]{3,24}$
  3. ^(?=^.{4,}$)(?=.*\d)(?=.*[az])(?=.*[AZ])(?!.*\s).*$

我该怎么做?
我在网上查看,但找不到任何具体的例子或答案.似乎也不存在可以自动执行此操作的实用程序.

How do I do this?
I looked on the web but couldn't find any concrete examples or answers. Also there seem to exist no utilities that could do this automatically.

我知道有时,这种转换并不准确,可能会产生差异,但我愿意尝试.

I am aware that some times, such conversions are not exact and can produce differences but I am willing to try.

推荐答案

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]'
    

  • 这篇关于将 PCRE 转换为 POSIX 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆