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

Regex to match x numbers and 1 letter

How can I match any amount of numbers and exactly 1 letter (the valid letters being d, m, y to signify date unit)

e.g. Valid

  • 1d
  • 30m
  • 1232y

e.g. Invalid

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

  • 1dd
  • 30mm
  • 1232yyy

I’ve tried a few things like [1-9]\b[a-zA-Z]\b and [1-9][a-zA-Z]

>Solution :

If the letter should be at the end

\b\d+[dmy]\b

Regex demo

To not match only zeroes like 00m

\b(?!0+[dmy]\b)\d+[dmy]\b

Regex demo

Note that [1-9] will not match a zero so 30m will not match in that case.

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