Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my regex expression matching multiple groups?

I want to apply a regex to a different strings that I have

all of them start with 3 CAP letters (XXX) and 6 number digits YYYYYY: XXXYYYYYY example: SBV087353

I want to match only the items that match that format of 3 CAP letters and 6 digit numbers.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Testing with an online tool I managed to create this regex: [A-Z]*[0-9]{1,6}

But if I type more numbers after the 6 digits or I type letters at the beginning it keeps matching it anyway.. why?

>Solution :

It’s because you are searching anywhere in string. Include ^ (start) and $ (end) markings.

Also specify exact length of letters and numbers, otherwise you will allow e.g. A1 (or with [A-Z]* even 9)

/^[A-Z]{3}\d{6}$/
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading