-3

Can anyone please let me know the regex code to enter digit(1 to 31) followed by one single character for example: 2d or 3d. The character will always remain 'd' or 'D' and number can be any number from 1 to 31. Basically this denotes 2 days or 3 days.

Raghav
  • 1

1 Answers1

1

StackOverflow is not a homework service.

But here you go...

\b(?:3[01]|[12][0-9‌​]|[1-9])[dD]\b

You can use http://regexr.com to test out regular expressions easily.

(Improved by Biffen)

Community
  • 1
  • 1
byxor
  • 5,930
  • 4
  • 27
  • 44
  • I'm pretty bad with regex and tend to avoid it so this definitely isn't the best example. I just provided something that works. Will fix and add disclaimer. :) – byxor Aug 16 '16 at 11:46
  • 2
    This can be shortened to `\b(?:3[01]|[12][0-9]|[1-9])[dD]\b`. – Biffen Aug 16 '16 at 11:50