SantanuSahoo SantanuSahoo - 5 months ago 23
MySQL Question

Regex - How two characters matching (MySql)

Please find my query below.

SELECT * FROM demoTable WHERE C_APPLICATION_REF_NO REGEXP '[A-Z]{1}[0-9]{0,14}';


why i am getting result which having two character whereas i am matching only one character and others are numbers.

enter image description here

how to modify my regex expression so that i can only fetch one character and others are numbers.

Answer

The reason is that your regex is not anchored to the start/end of string, and thus partial matches are returned, too.

Use

REGEXP '^[A-Z][0-9]{0,14}$'

{1} is redundant.