Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regex in MySQL?

Tags:

regex

mysql

I want to filter out those with field not like '%_[0-9]+' ,

but it turns out that MySQL doesn't take it as regex,

is that possible in MySQL?

like image 588
wireshark Avatar asked May 20 '11 04:05

wireshark


People also ask

Does MySQL like use RegEx?

Use the LIKE or NOT LIKE comparison operators instead. The other type of pattern matching provided by MySQL uses extended regular expressions. When you test for a match for this type of pattern, use the REGEXP_LIKE() function (or the REGEXP or RLIKE operators, which are synonyms for REGEXP_LIKE() ).

How do you use RegEx?

If you want to match for the actual '+', '. ' etc characters, add a backslash( \ ) before that character. This will tell the computer to treat the following character as a search character and consider it for matching pattern. Example : \d+[\+-x\*]\d+ will match patterns like "2+2" and "3*9" in "(2+2) * 3*9".

Can you use RegEx in SQL like?

You can use RegEx in many languages like PHP, Python, and also SQL.


1 Answers

That happens because of LIKE is not supposed to accept regular expression as a parameter. There is REGEXP for such things

WHERE field NOT REGEXP '%_[0-9]+'
like image 142
zerkms Avatar answered Sep 20 '22 20:09

zerkms