简体   繁体   English

识别罗马数字后跟“.”、空格和大写字母。 (正则表达式)

[英]Recognize roman numeral followed by '.', space and then capital letter. (RegEx)

Can someone please help me with this?有人可以帮我吗?

I'm trying to match roman numerals with a "."我正在尝试将罗马数字与“。”匹配。 at the end and then a space and a capital letter after the point.在末尾,然后是一个空格和一个大写字母。 For example:例如:

I. And here is a line. I. 这是一条线。

II.二、 And here is another line.这是另一条线。

X. Here is again another line. X. 这里又是另一行。

So, the regex should match the "I. A" , "II. A" and "X. H" .因此,正则表达式应该匹配"I. A""II. A""X. H"

I did this "^(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}){1,4}\.\s[AZ]" But the problem is that this RegEx is also matching with ". A" and i don't want it.我做了这个"^(XC|XL|L?X{0,3})(IX|IV|V?I{0,3}){1,4}\.\s[AZ]"但问题是这个正则表达式也与". A"匹配,我不想要它。

In resume it should have at least one roman numeral, followed by a "."在简历中,它应该至少有一个罗马数字,后跟一个"." and then a space and a capital letter.然后是一个空格和一个大写字母。

You need a (?=[LXVI]) lookahead at the start that would require at least one Roman number letter at the start of the string:您需要在开头有一个(?=[LXVI])前瞻,在字符串的开头至少需要一个罗马数字字母:

^(?=[LXVI])(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\.\s[A-Z]
# ^^^^^^^^^

See the regex demo .请参阅正则表达式演示 Not sure why you used {1,4} , I suggest removing it.不知道你为什么使用{1,4} ,我建议删除它。

Another workaround here would be to use a word boundary right after ^ :这里的另一个解决方法是在^之后使用单词边界:

^\b(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\.\s[A-Z]
#^^

This would disallow a match where .这将不允许匹配 where . appears at the start since \b , required at the same position as the start of string, requires that the next char must be a word char (and here, it must be a Roman number).\b以来出现在开头,需要与字符串开头相同的 position ,要求下一个字符必须是单词 char (这里,它必须是罗马数字)。

Regarding \.\s[AZ] , you may enhance it you add + or * after \s , and if you ever need to match it and exclude from a match, turn it into a positive lookahead, (?=\.\s+[AZ]) or (?=\.\s*[AZ]) .关于\.\s[AZ] ,您可以在\s之后添加+*来增强它,如果您需要匹配它并从匹配中排除,请将其转换为积极的前瞻, (?=\.\s+[AZ])(?=\.\s*[AZ])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 正则表达式查找小写字母后跟大写字母 - python regex find lowercase followed by capital letter 正则表达式为字母,后跟空格和引号(“) - Regex for a letter followed by space and quotations ( ") 如何在没有空格的情况下对大写字母进行正则表达式| Python 3 - How to regex Capital Letter without space before | Python 3 大写字母的正则表达式后跟一个空格,后跟数字“ ABC 123”或“ BLZ 420” - Regex for capital letters followed be a space followed by numbers 'ABC 123' or 'BLZ 420' 正则表达式:在数字后跟字母时添加空格 - Regex: Add space after a number when followed by letter 正则表达式匹配3个大写字母后跟一个小写字母后跟3个大写字母? - Regular expression to match 3 capital letters followed by a small letter followed by 3 capital letters? python regex字母后必须跟另一个字母 - python regex letter must be followed by another letter 如何在Python中获取第一个大写字母,然后再获取每个不跟另一个大写字母的字母? - How to get the first capital letter and then each that isn't followed by another capital letter in Python? 仅当带空格,句点或什么都没有正则表达式时,才使用Python匹配字符串中的字母吗? - Use Python to match a letter in a string only when followed by a space, period, or nothing, without regex? Python Regex - 检查大写字母后面的大写字母 - Python Regex - checking for a capital letter with a lowercase after
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM