Home > Net >  Regex Explanation required
Regex Explanation required

Time:02-03

My Regex:

\d{0,2}\.?\d{0,3}r?em

string to match:

12123rem

Why is it matching 5 digits?

I want it to match the following pattern only: 12em or 12 rem or 12.345em or 12.345rem

up to 3 decimal places only.

CodePudding user response:

Try this one:

^\d{0,2}(\.\d{1,3})?\s?r?em$

Here is a demo

  • ^ - assures we are at the begining of the string
  • ^\d{0,2} zero up to two decimals after begining of string
  • (\.\d{1,3}) optional group of 1 up to 3 digits with . in front
  • \s? optional whitespace character
  • r? optional r character
  • em mandatory em
  • $ end of the string
  • Related