将 Perl 正则表达式转换为 SQL 正则表达式
Convert Perl regular expressions to SQL regular expressions
我需要在 Firebird 数据库中使用 Perl 正则表达式。
Firebird RDBMS 通过提供 SIMILAR TO
-condition 支持正则表达式。
不幸的是,SQL Firebird 中的正则表达式语法与 Perl 语法不同。
是否可以将 Perl 正则表达式转换为 SQL 正则表达式?
我不需要完全兼容,但至少量词和字符 类 应该是可转换的。
[I] do not need full compatibility but at least quantifiers and character classes should be convertible.
运气好,可以用字符类、?
、*
、+
、{exact_occurrences_number}
、{min,}
、{min,max}
带有 Firebird SIMILAR TO
regex syntax.
的量词
唯一的问题是 Unicode category/property 类,你只能使用 POSIX 字符 类 那里:
<predefined class name> ::= ALPHA | UPPER | LOWER | DIGIT
| ALNUM | SPACE | WHITESPACE
我得出以下替换规则(顺序很重要)将大多数 Perl 正则表达式转换为 SQL 语法:
首先,SQL个特殊字符需要转义:
_
> \_
%
> \%
那么,Perl特殊字符和字符类就得替换了。
.
> _
\d
> [:digit:]
\D
> [^[:digit:]]
\w
> [^[:whitespace:]]
\W
> [:whitespace:]
\s
> [:whitespace:]
\S
> [^[:whitespace:]]
注意:默认的 Perl 转义字符 \
用于此处的 SIMILAR TO
。
欢迎使用更多可能的替代品来扩展我的答案。
我需要在 Firebird 数据库中使用 Perl 正则表达式。
Firebird RDBMS 通过提供 SIMILAR TO
-condition 支持正则表达式。
不幸的是,SQL Firebird 中的正则表达式语法与 Perl 语法不同。
是否可以将 Perl 正则表达式转换为 SQL 正则表达式? 我不需要完全兼容,但至少量词和字符 类 应该是可转换的。
[I] do not need full compatibility but at least quantifiers and character classes should be convertible.
运气好,可以用字符类、?
、*
、+
、{exact_occurrences_number}
、{min,}
、{min,max}
带有 Firebird SIMILAR TO
regex syntax.
唯一的问题是 Unicode category/property 类,你只能使用 POSIX 字符 类 那里:
<predefined class name> ::= ALPHA | UPPER | LOWER | DIGIT
| ALNUM | SPACE | WHITESPACE
我得出以下替换规则(顺序很重要)将大多数 Perl 正则表达式转换为 SQL 语法:
首先,SQL个特殊字符需要转义:
_
>\_
%
>\%
那么,Perl特殊字符和字符类就得替换了。
.
>_
\d
>[:digit:]
\D
>[^[:digit:]]
\w
>[^[:whitespace:]]
\W
>[:whitespace:]
\s
>[:whitespace:]
\S
>[^[:whitespace:]]
注意:默认的 Perl 转义字符 \
用于此处的 SIMILAR TO
。
欢迎使用更多可能的替代品来扩展我的答案。