mysqludf/lib_mysqludf_preg

Weird behavior with master

Closed this issue · 1 comments

SELECT PREG_CAPTURE('/[0-9]*/','AL02814'); => ''
SELECT PREG_CAPTURE('/[0-9]+/','AL02814'); => '02814'
SELECT PREG_CAPTURE('/[0-9]*/','02814AL'); => '02814'
SELECT PREG_CAPTURE('/[0-9]+/','02814AL'); => '02814'

I don't understand the first result. Is there something I forget?

Regards,

jasny commented

PCRE will start with at the beginning of the string, try to match the regex and if it succeeds return the found string. Otherwise it will try the next character

  • AL02814 matches? -> return match
  • L02814 matches? -> return match
  • 02814 matches? -> return match
  • 2814 matches? -> return match
  • 814 matches? -> return match
  • 14 matches? -> return match
  • 4 matches? -> return match

With /[0-9]*/ the first case actually matches. (You're looking for 0 or more digits, it has found 0.) So it returns the found match, which is an empty string.

See https://www.regex101.com/r/52ILLZ/1