开发者

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',开发者_如何学Go '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


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.


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}$';


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜