MySQL RegExp: Why Are These 2 Statements Giving The Same Result?

Viewed 18

Please consider the two statements below:

SELECT 'abc123456def' REGEXP '^[^0-9]+[0-9]{1,4}[^0-9]+$' AS Test;

Returns: 0

My Understanding is that the anchors are what cause the statement to return 0 because the string contains more numerals than the maximum of 4 that is specified in the pattern.

But then:

SELECT 'abc123456def' REGEXP '[^0-9]+[0-9]{1,4}[^0-9]+' AS Test;

Also Returns: 0

I expected this second statement to return 1. From what I've learnt about RegExp thus far, the string matches the pattern. Note that there are no anchors.

To test my understanding further,

SELECT 'abc123456def' REGEXP '^[^0-9]+[0-9]{1,6}[^0-9]+$' AS Test;

Returns: 1 - as it should.

So what am I missing in the 2nd statement?

Fiddle

Update In a previous question, someone explained that without anchors, "REGEXP returns true if the expression matches part of the text. The previous question - MySQL RegularExpression (Limiter not working)

0 Answers
Related