3

I am stuck in between of a problem where only one pass of regular expression is allowed( some old hard code). I need the regex for roman numerals.

I have tried the standard one i.e. ^(?i)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$, but the problem is it allows null('') values also.

Is there any way around to check is problem?

kapa
  • 77,694
  • 21
  • 158
  • 175
Nains
  • 258
  • 2
  • 15

2 Answers2

2

To require that at least one character must be present, you can use a lookahead (?=.) at the start of your regular expression:

^(?=.)(?i)M*(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$

Another solution is to separately test that your string is not the empty string.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

I like this one:

\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\b
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94