开发者

Single Regex for filtering roman numerals from the text files

开发者 https://www.devze.com 2022-12-25 10:56 出处:网络
I am stuck in between of a problem where o开发者_开发知识库nly one pass of regular expression is allowed( some old hard code). I need the regex for roman numerals.

I am stuck in between of a problem where o开发者_开发知识库nly 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?


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消