Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL regex in the where clause

Tags:

regex

mysql

SELECT telephone_number FROM table WHERE telephone_number REGEXP '^1[() -]*999[() -]*999[() -]*9999$'; 

how do i make so its valid for any number format and any number like

407-888-0909 1(408)998-7654 7776654433 876-7788 

right now its only valid for 1-999-999-9999

like image 315
Matt Elhotiby Avatar asked Aug 10 '10 19:08

Matt Elhotiby


People also ask

What is the correct syntax to use regular expression in query?

Basic syntax“WHERE fieldname” is the name of the column on which the regular expression is to be performed on. “REGEXP 'pattern'” REGEXP is the regular expression operator and 'pattern' represents the pattern to be matched by REGEXP. RLIKE is the synonym for REGEXP and achieves the same results as REGEXP.

How do I match a pattern in MySQL?

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() ).


1 Answers

Use:

SELECT telephone_number   FROM table  WHERE telephone_number REGEXP '^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$'; 

Reference:

  • Pattern Matching
like image 64
OMG Ponies Avatar answered Sep 28 '22 18:09

OMG Ponies