IAbstract
IAbstract

Reputation: 19881

What is the MySql regex equivalent for

In .Net, I use ^[BG]\d(0-9)4$ for a Regex.IsMatch ( ... ) comparison. I am looking for the equivalent regex in MySql. Any help?

EDIT
corrected the regex used in .Net - it should only allow 'b', 'B', 'g', or 'G' at the beginning.

allowed samples:
- b1234
- B2254
- g5534
- G1122

disallowed:
- any letter besides b, B, g, G
- fewer than 4 digits following b/B/g/G
- greater than 4 digits following b/B/g/G

Upvotes: 0

Views: 433

Answers (3)

King Skippus
King Skippus

Reputation: 3826

Edited for updated expression and examples in question.

Try:

REGEXP '^[BG]\d{4}$'

*Disclaimer: I'm not where I have access to a MySQL server now, so that answer is based on the MySQL documentation, not actually typing it in and trying it. Your mileage may vary.

Upvotes: 0

anubhava
anubhava

Reputation: 785296

Not sure what you are trying to match but following queries match fine for me:

SELECT 'B544' REGEXP '^[B,G][0-9][0-9]4$';
SELECT ',544' REGEXP '^[B,G][0-9][0-9]4$';
SELECT 'G544' REGEXP '^[B-G][0-9][0-9]4$';

EIDT:

Based on your comments you should use regex '^[BG][0-9]{4}$' here are examples:

-- doesn't match
SELECT 'a1234' REGEXP '^[BG][0-9]{4}$';
SELECT 'B12345' REGEXP '^[BG][0-9]{4}$';
SELECT 'G123' REGEXP '^[BG][0-9]{4}$';

-- matches
SELECT 'B1234' REGEXP '^[BG][0-9]{4}$';
SELECT 'G1122' REGEXP '^[BG][0-9]{4}$';

Upvotes: 0

Denis de Bernardy
Denis de Bernardy

Reputation: 78483

Doesn't your current regex reject all examples? If they're anyhting to go by you want:

^[BG][0-9]{4}$`

Which should work in your app and MySQL.

Upvotes: 1

Related Questions