Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match optional end of line

Tags:

regex

mysql

Hey I want to use a regular expression in MySQL to match rows. It needs to match rows where a the pattern ends with anything that's not a digit or the end of the line.

This pattern works in Ruby /download:223(?:[\D]|$)/ In MySQL it doesn't match. I'm guessing it doesn't allow for optional matching of eol.

SELECT id FROM stories WHERE body REGEXP 'download:223(?:[\D]|$)'

I need to match the following (quotes just for clarity):

"download:223"
"download:223*"
"download:223 something"
"download:223 more text"

But NOT the following (again quotes just for clarity):

"download:2234"
"download:2234 more text"
"download:2234*"
"download:2234* even more"

Thanks!

like image 807
Rob Avatar asked Dec 20 '25 15:12

Rob


1 Answers

This regex should work for you:

"download:223([^0-9]|$)"

MySQL regex engine doesn't support \D, \d etc.

like image 193
anubhava Avatar answered Dec 22 '25 12:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!