Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的正则表达式,用于删除冒号前的所有大写字符_Python_Regex_String_Text_Re - Fatal编程技术网

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
    防止部分匹配的单词边界
  • [A-Z]+:
    匹配1+大写字符A-Z和A
  • \s*
    匹配可选空白字符

输出

This is a text. This is a second text. This is a third text

哦,这个演示网站很方便,干杯!很好的解释,非常清楚您可以使用
+?
*?
(lazy regex)查找最小的匹配字符串。从所需的输出中,我们可以看到您删除了连续的大写字母、冒号和至少一个空格。你能详细说明一下目标吗clearly@MarkSouls但这并不能解决问题,因为在本例中,它与大写字符不匹配,并且锚点阻止了多个字符matches@Thefourthbird是的,我只是把它作为一种相关的提示。