user2783755
user2783755

Reputation: 588

Regex issue on SQL

Why do I get 0 when running this expression?

SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1+[ ,.-/\]*7+[ ,.-/\]*0+[ ,.-/\]*0+[ ,.-/\]*9+[ ,.-/\]*0+[ ,.-/\]*2+[ ,.-/\]*8+[ ,.-/\]*4+[ ,.-/\]*2+[ ,.-/\]*3+';

I need to get true, when the text contains the specified number (17009028423). There can be symbols ,.-/\ between digits. For example, if I have number 17009028423, I need get true when in text is:

Thanks.

Upvotes: 1

Views: 45

Answers (1)

Mifeet
Mifeet

Reputation: 13608

There are two problems with your regular expression. First is that backslash in \] escapes the special meaning of ] to denote a character class. You need to escape your backslash: \\]. Another problem is that - denotes a range [ and ] (e.g. [a-zA-Z]). You need to escape that too or put it at the end like [a-zA-Z-] (as @tenub said). Plus the backslashes should be escaped themselves, which makes:

SELECT 'Nr. 1700-902-8423. asdasdasd' REGEXP '1[ ,./\\\\-]*7[ ,./\\\\-]*0[ ,./\\\\-]*0[ ,./\\\\-]*9[ ,./\\\\-]*0[ ,./\\\\-]*2[ ,./\\\\-]*8[ ,./\\\\-]*4[ ,./\\\\-]*2[ ,./\\\\-]*3'

You can check for yourself. I also removed + signs in case you want to match each number only once.

Upvotes: 3

Related Questions