Python中的正则表达式,用于删除冒号前的所有大写字符
我有一个文本,我想删除所有大写连续字符直到冒号。我只知道如何删除冒号之前的所有字符;这将产生如下所示的电流输出 输入文本Python中的正则表达式,用于删除冒号前的所有大写字符,python,regex,string,text,re,Python,Regex,String,Text,Re,我有一个文本,我想删除所有大写连续字符直到冒号。我只知道如何删除冒号之前的所有字符;这将产生如下所示的电流输出 输入文本 text = 'ABC: This is a text. CDEFG: This is a second text. HIJK: This is a third text' 所需输出: 'This is a text. This is a second text. This is a third text' re.sub(r'^.+[:]', '', text) #
text = 'ABC: This is a text. CDEFG: This is a second text. HIJK: This is a third text'
所需输出:
'This is a text. This is a second text. This is a third text'
re.sub(r'^.+[:]', '', text)
#current output
'This is a third text'
当前代码和输出:
'This is a text. This is a second text. This is a third text'
re.sub(r'^.+[:]', '', text)
#current output
'This is a third text'
这可以用一行正则表达式来完成吗?或者我需要遍历每个字符。isupper()
,然后实现正则表达式吗?您可以使用
\b[A-Z]+:\s*
防止部分匹配的单词边界\b
匹配1+大写字符A-Z和A[A-Z]+:
:
匹配可选空白字符\s*
This is a text. This is a second text. This is a third text
哦,这个演示网站很方便,干杯!很好的解释,非常清楚您可以使用
+?
或*?
(lazy regex)查找最小的匹配字符串。从所需的输出中,我们可以看到您删除了连续的大写字母、冒号和至少一个空格。你能详细说明一下目标吗clearly@MarkSouls但这并不能解决问题,因为在本例中,它与大写字符不匹配,并且锚点阻止了多个字符matches@Thefourthbird是的,我只是把它作为一种相关的提示。