2

My regular expression is this:

((?:[a-z][a-z0-9_]*)).*?(\d+).*?((?:[a-z][a-z0-9_]*)).*?(\d+).*?([a-z])

If I give this as input to MySQL RegExp I get the repetition operator error.I know it occurs because of ?: and I replaced it with ^ and I also replace * ? with [^>]* an my replaced regular expression is this:

'((^[a-z][a-z0-9_]*)).[^>]*.(\\d+).[^>]*.((^[a-z][a-z0-9_]*)).[^>]*.(\\d+).[^>]*.([a-z])'

The above expression executes with no errors but the matching fails and returns wrong results.So I want to convert the first regular expression to a POSIX standard supported by mysql without loosing the constraints.

3
  • MySQL does not support .*? lazy quantifiers Commented Jun 6, 2014 at 7:59
  • Yeah that's why I replaced it with [^>]*. Commented Jun 6, 2014 at 8:00
  • FYI, added explanation about the changes to convert the expression. Commented Jun 6, 2014 at 8:06

1 Answer 1

6

The most important transformation needed is mimicking the lazy quantifier, which does not exist in MySQL. To do this, as you'll see in point 3 of the explanation, we will use mutually-incompatible expressions.

This should do it:

(([a-z][a-z0-9_]*))[^0-9]*([0-9]+)[^a-z]*(([a-z][a-z0-9_]*))[^0-9]*([0-9]+)[^a-z]*([a-z])

Unless you plan to use them, you can also get rid in capturing parentheses in places such as ([0-9]+) (where you had (\d+) before.)

Explanation

  1. Replaced the (?: non-capturing groups with regular groups
  2. Replaced \d with [0-9]
  3. Replaced the lazy quantifiers with a negated character class that negates what is to follow, for instance [^a-z]* precedes [a-z], and [^0-9]* precedes [0-9]
Sign up to request clarification or add additional context in comments.

Comments

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.