Home > Software design >  Like Function to RegexP
Like Function to RegexP

Time:12-19

Basically I have below query.

Select * from testtabel
     where call_id like 'u34567' 
        or call_id like 'q34576' or ..... ;

7534567,7134576 are not any pattern. So I need to check any call_id which ends 7534567 or 7134576. since I have many to check without using LIKE can I use regexp to check that?

refered Using multiple values in mySQL regexp and I tried as below,

Select * from testtabel  where call_id REGEXP '^(7534567|7134576)$';

But this provides zero records. Can someone show me how should I do that?

CodePudding user response:

Did you try like this to match multiple patterns using REGEXP?

SELECT * FROM testtabel WHERE call_id REGEXP '(7534567|7134576)$'  ;

CodePudding user response:

The ^ anchor matches (here, requires the match at) the start of the string.

You need to use

where call_id REGEXP '([^0-9]|^)(7534567|7134576)$'

Details:

  • ([^0-9]|^) - any non-digit char or start of string
  • (7534567|7134576) - 7534567 or 7134576 char sequence
  • $ - end of string.

See the regex demo.

  • Related