0

String will be consider valid if it starts with letters MS and must contain numeric values 0-9, maximum numeric values allowed are 7.

Valid strings :

MS1234567

MS3434344

MS4534523

Invalid sting:

  MS1234567-V2

    MS3434344:old

    YU4534523

    MS4534523768

TY4534523DEL

This the query I tried, I am getting blank result. Where I am going wrong

SELECT MY_STRING_COLUMN
FROM MY_TABLE_NAME
WHERE  `MY_STRING_COLUMN` REGEXP '^[MS]{2}\d{7}';

Reference I got from this Stackoverflow post

6
  • 2
    Remove the brackets and the quantifier {2}, add anchor - ^MS\d{7}$ (MySQL 8+) / ^MS[0-9]{7}$ (MySQL before 8). [MS] only matches a single char, M or S, thus [MS]{2} matches MM, SS, SM, or MS. Without $, you may match the pattern just at the start of the string, but there may be much more than that later. Commented Jul 5, 2019 at 13:54
  • @WiktorStribiżew I'm not sure that \d works with MySQL's REGEXP. Commented Jul 5, 2019 at 13:55
  • @TimBiegeleisen It will in MySQL 8.x Commented Jul 5, 2019 at 13:56
  • How do you know the OP is using MySQL 8+? Commented Jul 5, 2019 at 13:57
  • A lot of people are migrating to the newer version. Commented Jul 5, 2019 at 13:58

1 Answer 1

1

Try using [0-9] to represent a digit in your regex pattern:

SELECT MY_STRING_COLUMN
FROM MY_TABLE_NAME
WHERE MY_STRING_COLUMN REGEXP '^MS[0-9]{7}$';

Also note that if you want to match MS at the start of the string, just use ^MS.

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

2 Comments

Thanks..let me run this REGEXP!
(Note: MySQL 8.0 and MariaDB do allow \d.)

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.