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

Regular expression to match first two characters

I need to write regex to match that first characters is "Am" or "x" and other is only digits (5 times). Here is my regex:

"[Am|x]\\d{5}".toRegex()

Full:

"Am11111".matches("[Am|x]\\d{5}".toRegex())

However, this regular expression does not work correctly. It defines matches separately on A, m and x. But I need Am and x.

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

For example:

Am11111 – this is must be correct

x11111 – this is must be correct

a11111 – this is incorrect

m11111 – this is incorrect too

Please, help me.

>Solution :

You almost got it, just use () instead of []
(Am|x)\\d{5}

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