question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

postgresql / sqlite / mysql /oracle regular expression operators

See original GitHub issue

Migrated issue, originally created by Anonymous

match operator not implemented

in sqlalchemy/databases/postgres.py 727 just add sql_operators.match_op: lambda x, y, escape=None: ‘%s ~ %s’ % (x, y) + (escape and ’ ESCAPE '%s'’ % escape or ‘’),

and it works for me CAVEAT sql might crash if improper regexp syntax entered.

  • missing imatch / notmatch …

(these matching functions are still used with same syntax) http://www.postgresql.org/docs/7.4/interactive/functions-matching.html


Attachments: 1390.patch


Edit 2020-07-10

Coping this recap from https://github.com/sqlalchemy/sqlalchemy/issues/5447#issue-654578670

The call column.regex_match('[a-z]*') would then evaluate to:

  • Postgres: column ~ "[a-z]*"
  • SQLite: column REGEXP "[a-z]*"
  • MySQL, MariaDB: column REGEXP "[a-z]*"
  • Oracle: REGEXP_LIKE(column, "[a-z]*")
  • MSSQL: throw exception

Issue Analytics

  • State:closed
  • Created 14 years ago
  • Comments:59 (29 by maintainers)

github_iconTop GitHub Comments

3reactions
CaselITcommented, Jul 12, 2020

Should we output the regex provided by the user verbatim, and warn them to use a portable subset of regex, or should we attempt to convert the regex to a backend-compatible version at execution time? (this sounds hard).

I think that a note on the documentation is enough in this case. It would be a lot of work to try and convert the regexp so I think that we should just render the input verbatim and let the db error if it is not correct

2reactions
multimericcommented, Jul 12, 2020

Okay, here’s a feature matrix:

Feature Postgres MySQL SQLite Oracle MSSQL
Does column match pattern? column ~ pattern column REGEXP pattern column REGEXP pattern (requires plugin, but has syntax support) REGEXP_LIKE(column, pattern) n/a
Does column match pattern using flags? column ~ (?flags)pattern REGEXP_LIKE(column, pattern, flags) n/a REGEXP_LIKE(column, pattern, flags) n/a
Replace pattern with replacement in column regexp_replace(column, pattern, replacement) REGEXP_REPLACE(column, pattern, replacement) n/a (can be loaded as a custom function, but no syntax exists for this) REGEXP_REPLACE(column, pattern, replacement) n/a
Replace pattern with replacement in column with flags regexp_replace(column, pattern, replacement , flags) REGEXP_REPLACE(column, pattern, replacement, 1, 0, flags) n/a REGEXP_REPLACE(column, pattern, replacement) n/a

Based on this, I think it makes sense to add the first row (“does column match regex”) as a core string-column function, but I’m undecided on the other two features. It would still be helpful to users to have them available in a database-independent manner, event though they are less well-supported.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MYSQL Regular Expressions (REGEXP) with Syntax ... - Guru99
“REGEXP 'pattern'” REGEXP is the regular expression operator and 'pattern' represents the pattern to be matched by REGEXP. RLIKE is the synonym ...
Read more >
SQL RegEx | Regular Expressions in MySQL with Examples
A Regular Expression is popularly known as RegEx, is a generalized expression that is used to match patterns with various sequences of ...
Read more >
MySQL REGEXP operator - w3resource
REGEXP operator. MySQL REGEXP performs a pattern match of a string expression against a pattern. The pattern is supplied as an argument.
Read more >
10 Using Regular Expressions in Database Applications
A regular expression specifies a search pattern, using metacharacters (which are, or belong to, operators) and character literals (described in Oracle Database ...
Read more >
Regular Expressions in PostgreSQL - Postgres OnLine Journal
PostgreSQL has a rich set of functions and operators for working with regular expressions. The ones we commonly use are ~, regexp_replace, and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found