0

I'm using a correct regex, already tested with other tools. But in mysql seems not to work properly. Say this data:

enter image description here

And I want to select all the records which start with 2 non-digits followed by a dot, followed by one digit followed by a sequence of .00 (ex. DE.1.00.00.00.00)

\D{2}\.\d?\.0{2}.0{2}.0{2}.0{2}

The above used alone normally works. But on mysql, a simple query like the following won't work. It will return a table with all the columns and without any error, but also without any value. Why?

SELECT *
FROM V35_CATEGORIE cat
WHERE cat.CategoriaCodice REGEXP '\D{2}\.\d?\.0{2}.0{2}.0{2}.0{2}'
asked Oct 16, 2019 at 15:12
1
  • What version of MySQL? Commented Oct 18, 2019 at 23:04

1 Answer 1

0

I answer by myself. In short, sql syntax above is correct, so how to query mysql tables with regular expressions? Exactly like I did.

The reason why it didn't work, is regexp syntax being too 'complex' for mysql. I found out that mysql doesn't support the full syntax possibilities of standard regular expressions. I must use simpler symbols/constructs. So for example, instead of \D{2} I can use [A-Z]{2}, and instead of \d, knowing I'll want 1 or 2, I can use [1,2]?. A syntax like the following will perfectly work in mysql to match a string like 'IT.1.12.00.00.00':

[A-Z]{2}\.[1,2]?\.[1-9][0-9](\.00){3}

With full regexp syntax I could have use lookahead assertions and other constructs, but anyway, for now for my needs I can use plain 'old' grammar.

answered Oct 17, 2019 at 10:13
1
  • The paragraph discusses what could be used in MySQL 8.0. The answer applies to any version. Commented Oct 18, 2019 at 23:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.