Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Text - Fatal编程技术网

Python 删除文本周围的括号,并在末尾添加冒号

Python 删除文本周围的括号,并在末尾添加冒号,python,regex,text,Python,Regex,Text,我有一个长字符串,希望替换此类事件: 'eggs (spam): tomatoes' 'eggs : spam tomatoes' 对于这种类型的: 'eggs (spam): tomatoes' 'eggs : spam tomatoes' 也就是说,如果存在“左括号、文本、右括号、双冒号、空格”类型的模式,那么我希望将其替换为“双冒号、空格、文本” 我试着写了以下内容: import re re.sub('\(.+\): ', '', 'eggs (spam): tomatoes')

我有一个长字符串,希望替换此类事件:

'eggs (spam): tomatoes'
'eggs : spam tomatoes'
对于这种类型的:

'eggs (spam): tomatoes'
'eggs : spam tomatoes'
也就是说,如果存在“左括号、文本、右括号、双冒号、空格”类型的模式,那么我希望将其替换为“双冒号、空格、文本”

我试着写了以下内容:

import re
re.sub('\(.+\): ', '', 'eggs (spam): tomatoes')

但是(毫不奇怪)它完全删除了括号内的文本,我不知道如何保留以前在函数的“替换”部分括号内的文本。

您应该使用捕获组:

re.sub(r"\(([^()]*)\)(:)", r"\2 \1", 'eggs (spam): tomatoes')

正则表达式细分:

  • \(
    匹配左括号
  • 开始捕获第一组
    • [^()]*
      匹配
  • 捕获组1结束
  • \)
    匹配右括号
  • (:)
    捕获冒号(CG#2)

替换字符串
“\2\1”
表示替换应在第二次捕获组数据之后进行,然后是一个空格,然后是第一次捕获组数据。

您应使用捕获组:

re.sub(r"\(([^()]*)\)(:)", r"\2 \1", 'eggs (spam): tomatoes')

正则表达式细分:

  • \(
    匹配左括号
  • 开始捕获第一组
    • [^()]*
      匹配
  • 捕获组1结束
  • \)
    匹配右括号
  • (:)
    捕获冒号(CG#2)
替换字符串
“\2\1”
意味着替换应该在第二次捕获组数据之后,然后是一个空格,然后是第一次捕获组数据。

使用
re.sub('\(.*?):',r':\1',鸡蛋(垃圾邮件):西红柿')

演示:

import re
print(re.sub('\((.*?)\): ', r':\1 ', 'eggs (spam): tomatoes'))
eggs :spam tomatoes
输出:

import re
print(re.sub('\((.*?)\): ', r':\1 ', 'eggs (spam): tomatoes'))
eggs :spam tomatoes
使用
re.sub('\(.*?):',r':\1',鸡蛋(垃圾邮件):西红柿')

演示:

import re
print(re.sub('\((.*?)\): ', r':\1 ', 'eggs (spam): tomatoes'))
eggs :spam tomatoes
输出:

import re
print(re.sub('\((.*?)\): ', r':\1 ', 'eggs (spam): tomatoes'))
eggs :spam tomatoes

在代码中,从左括号到右括号(包括冒号)进行选择,并将其替换为空字符串。这就是它完全删除括号内文本的原因

您可以使用2个捕获组并替换为组2组1:

  • \(
    逐字匹配
  • (.+?)
    在组1中捕获任何字符一次或多次
  • \)
    逐字匹配
  • (:)
    在组2中捕获冒号
    \\2
例如:

import re
print(re.sub(r"\((.+?)\)(:)", "\\2 \\1", 'eggs (spam): tomatoes'))
这将给你:

eggs : spam tomatoes

在代码中,从开始的圆括号中选择,直到结束的圆括号中包含冒号,并用空字符串替换。这就是它完全删除括号内文本的原因

您可以使用2个捕获组并替换为组2组1:

  • \(
    逐字匹配
  • (.+?)
    在组1中捕获任何字符一次或多次
  • \)
    逐字匹配
  • (:)
    在组2中捕获冒号
    \\2
例如:

import re
print(re.sub(r"\((.+?)\)(:)", "\\2 \\1", 'eggs (spam): tomatoes'))
这将给你:

eggs : spam tomatoes

这项工作:

>>> re.sub('\((.*)\): ', ': \\1 ', 'eggs (spam): tomatoes')
eggs : spam tomatoes
这项工作:

>>> re.sub('\((.*)\): ', ': \\1 ', 'eggs (spam): tomatoes')
eggs : spam tomatoes

您可以使用
re.findall
re.sub

import re
s = 'eggs (spam): tomatoes'
new_s = re.sub('\(\w+\):', '{}', s).format(*[f': {i}' for i in re.findall('\((.*?)\)', s)])
输出:

'eggs : spam tomatoes'

您可以使用
re.findall
re.sub

import re
s = 'eggs (spam): tomatoes'
new_s = re.sub('\(\w+\):', '{}', s).format(*[f': {i}' for i in re.findall('\((.*?)\)', s)])
输出:

'eggs : spam tomatoes'

我刚刚试着运行它,得到了这个输出
“鸡蛋$2$1西红柿”
我刚刚试着运行它,得到了这个输出
“鸡蛋$2$1西红柿”