Question

I'm very new to REGEX, so I apologize if this is too easy of a question. I'm working in MySQL and and trying to create a comparison which will yield results like this:

First four to six must match, if digits -

  • '414720' matches '414720xxxx1234'

  • '414720' matches '4147xxxxxx1234'

  • '414720' matches '41472***abcd'

  • '414720' matches '4147'

No match if other digit, or less than 4

  • '414720' does not match '414799xxxx1234'

  • '414720' does not match '414xxxxxxx1234'

I thought maybe I could get it with something like

SELECT '414720' REGEXP '4147[^0-9-[2]][^0-9-[0]]*'

but it returns /* SQL Error (1139): Got error 'invalid character range' from regexp */, I think because MySQL's REGEX doesn't support class subtraction. I think I could generate the explicit negative groups of [^1,3-9], but that seems like it would be tricky and I feel like I'm probably missing a much simpler answer. Any help would be greatly appreciated. Thanks.

edit: The x's above could be substituted for any character, or not present possibly. Added second two examples of matches.

Était-ce utile?

La solution

I don't know if it's simpler, but you can use:

4147($|\D|2($|\D|0)).*$

as shown in http://rubular.com/r/ehDtDaULZe. I wasn't sure what restrictions, if any, there were on the sixth character if the fifth character wasn't a digit.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top