简体   繁体   English

将Perl正则表达式转换为SQL正则表达式

[英]Convert Perl regular expressions to SQL regular expressions

I need to use Perl regular expressions in a Firebird database. 我需要在Firebird数据库中使用Perl正则表达式。

Firebird RDBMS does support regular expressions by providing a SIMILAR TO -condition . Firebird RDBMS通过提供SIMILAR TO condition来支持正则表达式。 Unfortunately, the SQL regular expression syntax in Firebird differs from Perl syntax. 不幸的是,Firebird中的SQL正则表达式语法与Perl语法不同。

Is it possible to convert Perl regular expressions to SQL regular expressions? 是否可以将Perl正则表达式转换为SQL正则表达式? I do not need full compatibility but at least quantifiers and character classes should be convertible. 我不需要完全兼容,但至少量词和字符类应该是可转换的。

[I] do not need full compatibility but at least quantifiers and character classes should be convertible. [I]不需要完全兼容,但是至少量词和字符类应该是可转换的。

You are lucky, you may use character classes, ? 您很幸运,可以使用字符类, ? , * , + , {exact_occurrences_number} , {min,} , {min,max} quantifiers with Firebird SIMILAR TO regex syntax . *+{exact_occurrences_number}{min,}{min,max}量词使用Firebird SIMILAR TO正则表达式语法

The only trouble are Unicode category/property classes, you may only use POSIX character classes there: 唯一的麻烦是Unicode类别/属性类,您只能在其中使用POSIX字符类:

<predefined class name> ::= ALPHA | UPPER | LOWER | DIGIT
| ALNUM | SPACE | WHITESPACE

I came to the following replacement rules (the order matters) to convert most Perl regular expressions to SQL syntax: 我遇到了以下替换规则(顺序很重要),可以将大多数Perl正则表达式转换为SQL语法:

At first, SQL special characters have to be escaped: 首先,必须转义SQL特殊字符:

  1. _ > \\_ _ > \\_
  2. % > \\% % > \\%

Then, Perl special characters and character classes have to be replaced. 然后,必须替换Perl特殊字符和字符类。

  1. . > _ > _
  2. \\d > [:digit:] \\d > [:digit:]
  3. \\D > [^[:digit:]] \\D > [^[:digit:]]
  4. \\w > [^[:whitespace:]] \\w > [^[:whitespace:]]
  5. \\W > [:whitespace:] \\W > [:whitespace:]
  6. \\s > [:whitespace:] \\s > [:whitespace:]
  7. \\S > [^[:whitespace:]] \\S > [^[:whitespace:]]

Note: The default Perl escape character \\ is used for SIMILAR TO here. 注意:默认的Perl转义字符\\在此处用于SIMILAR TO

Feel free, to extend my answer with further possible replacements. 请随时提出更多可能的替代方案,以扩展我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM