简体   繁体   中英

Convert Perl regular expressions to SQL regular expressions

I need to use Perl regular expressions in a Firebird database.

Firebird RDBMS does support regular expressions by providing a SIMILAR TO -condition . Unfortunately, the SQL regular expression syntax in Firebird differs from Perl syntax.

Is it possible to convert Perl regular expressions to SQL regular expressions? 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.

You are lucky, you may use character classes, ? , * , + , {exact_occurrences_number} , {min,} , {min,max} quantifiers with Firebird SIMILAR TO regex syntax .

The only trouble are Unicode category/property classes, you may only use POSIX character classes there:

<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:

At first, SQL special characters have to be escaped:

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

Then, Perl special characters and character classes have to be replaced.

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

Note: The default Perl escape character \\ is used for SIMILAR TO here.

Feel free, to extend my answer with further possible replacements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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