2

I have a regular expression intended to catch phone numbers within a string

/[1,+ ().-][\s.-]\d{3}[\s.-]\d{3}[\s.-]\d{4}/

I try to query my MySql database for this regex using this query:

SELECT
    *
FROM
    `everything`.`instances_meta` AS m
WHERE
    `meta.value` REGEXP "[1,+][\s.-]\d{3}[\s.-]\d{3}[\s.-]\d{4}" 

I can confirm the regex, itself, does what I want from testing it here but it does not pull any matches when queried. Something tells me I am missing some sort of expression to say "anywhere in the string". I would specify the regex engine but I am unsure of what MySql uses.

2
  • Reference: dev.mysql.com/doc/refman/5.7/en/regexp.html Commented Oct 3, 2016 at 21:04
  • Thanks for sharing a status report. (You are unsure what MySQL uses for a Regular Expression engine. That is documented in the MySQL Reference Manual, which is available.) But what I don't see is an actual question. Commented Oct 3, 2016 at 21:11

1 Answer 1

4

Use

"[1,+][[:blank:].-][0-9]{3}[[:blank:].-][0-9]{3}[[:blank:].-][0-9]{4}"

as MySQL REGEXP does not support \s and \d.

The [:blank:] matches spaces and tabs, and [0-9] matches any ASCII digit.

Sign up to request clarification or add additional context in comments.

5 Comments

FYI: MySQL REGEXP is not anchoring the pattern, and looks for a match anywhere inside the string (called a partial match). MySQL uses a POSIX regex flavor, a very limited set of regular expression patterns, however, the POSIX character classes are also fine to use. Note you might want to use [[:digit:]] instead of \d, or [:space:] instead of [:blank:] - test and see which is performing best.
Where do you find this information about what REGEXP support in MySQL? I'm having the same problem and the documentation not mention these limitations.
@Dherik What is the MySQL version you are using? Older versions before MySql v.8 used a POSIX regex engine so you may check all POSIX regex related reference.
I'm using the 5.7 version

Your Answer

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.