Single Regex to filter Roman numerals from text files

I am stuck between a problem where only one regex pass is allowed (some kind of old hard code). I need a regex for Roman numerals.

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

Is there any way to check the problem?

+3
source share
2 answers

To require the presence of at least one character, you can use lookahead (?=.) at the beginning 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 verify that your string is not an empty string.

+2
source

I like this:

\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
+1

Source: https://habr.com/ru/post/1739829/


All Articles