1

What is the best way for me to search this type of value via regular expression in MySQL?

"ABCDE/+19876543210@abc-def"

ABCDE/ represents a specific value that does not change

@abc-def also represents specific value that does not change

My unsuccessful attempt below:

SELECT BLAH
FROM BLOOP
WHERE (bloop.field REGEXP'^\\ABCDE/+1(123|234|345|456)[1-9]{7}@abc-def$')
3
  • you really want the 3 digit followed by digit 1 to be either of these (123|234|345|456) ? Commented Jan 8, 2019 at 17:14
  • yes. I am looking for phone numbers within specific area codes
    – JDoc
    Commented Jan 8, 2019 at 17:15
  • Bug: [1-9] --> [0-9]
    – Rick James
    Commented Jan 8, 2019 at 19:02

2 Answers 2

1

You didn't escaped +. it has a special meaning in regex which means one or more time.

You can use this one

^ABCDE\/\\+1(123|234|345|456)[1-9]{7}@abc-def$

Demo

0

The + is a regexp metacharacter, so you have to escape it if you want to match a literal +. And because \ is a string metacharacter, you have to escape it too.

mysql> select 'ABCDE/+19876543210@abc-def' regexp 
  '^ABCDE/\\+1(123|234|345|456)[0-9]{7}@abc-def$' as is_match;
+----------+
| is_match |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select 'ABCDE/+12346543210@abc-def' regexp 
  '^ABCDE/\\+1(123|234|345|456)[0-9]{7}@abc-def$' as is_match;
+----------+
| is_match |
+----------+
|        1 |
+----------+

I don't know why you had \\ in your regexp pattern, because that doesn't match the sample string.

I'm not sure what your digit triples are about. Are they phone area codes? Are you planning to list every area code?

3
  • I am looking to match specific area codes and then the remaining 7 digits can vary.
    – JDoc
    Commented Jan 8, 2019 at 17:18
  • Okay I have updated my answer showing a non-match and a match, based on the selected area codes. That's copy & pasted from a test I ran in MySQL 5.6.37, to prove it works. Commented Jan 8, 2019 at 18:07
  • Thank you so much!
    – JDoc
    Commented Jan 8, 2019 at 18:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.