Search code examples
regexroman-numerals

Single Regex for filtering roman numerals from the text files


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?


Solution

  • 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.