Rob
Rob

Reputation: 7099

Match optional end of line

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!

Upvotes: 3

Views: 312

Answers (2)

sdanzig
sdanzig

Reputation: 4500

Non-capturing groups are not supported in MySQL regexes. The rest should be fine. It definitely supports $ matching the end of string. Also, \D is not supported, but you can use [^0-9]

Try this:

SELECT id FROM stories WHERE body REGEXP 'download:223([^0-9]|$)'

MySQL groups don't capture, so supporting non-capturing groups is unnecessary.

Reference source: Using Non-Capturing Groups in MySQL REGEXP

Upvotes: 1

anubhava
anubhava

Reputation: 785541

This regex should work for you:

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

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

Upvotes: 1

Related Questions