Home > front end >  Regex to match x numbers and 1 letter
Regex to match x numbers and 1 letter

Time:05-24

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

  • 1dd
  • 30mm
  • 1232yyy

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

CodePudding user response:

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.

  • Related