正则表达式,返回括号之间有1个或多个句点的文本

时间:2018-08-06 21:09:33

标签: regex python-3.x

我的文本在括号之间有1个或多个2个句点。

K= 'Product will be hot(These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required).'

我想提取或消除整个文本。我已经尝试过

re.search(r'\((.*?)+\)',K).group(1) 

K[K.find("(")+1:K.find(")")]

但它们都不返回文本

3 个答案:

答案 0 :(得分:1)

您可以使用表达式:

(?<=\()[^()]*(?=\))

尝试实时表达here

使用re.findall查找您感兴趣的文本。

import re
K = 'Product will be hot(These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required).'
print(re.findall(r'(?<=\()[^()]*(?=\))',K))

打印:

['These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required']

或者将字符集包装到捕获组中

import re
K = 'Product will be hot(These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required).'
print(re.search(r'(?<=\()([^()]*)(?=\))',K).group(1))

打印:

These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required

答案 1 :(得分:1)

IIUC,以下正则表达式将删除包含一个或多个句点的括号之间的任何文本,以及括号本身:

re.sub('\(.*?\.+.*\)','', K)

示例:

>>> re.sub('\(.*?\.+.*\)','', K)
'Product will be hot.'

要提取文本而不是删除文本,请使用带有相同正则表达式的re.findall

>>> re.findall('\(.*?\.+.*\)', K)
['(These cooking instructions were developed using an 100 watt microwave oven. For lower wattage ovens, up to an additional 2 minutes cooking time may be required)']

[编辑] :若要匹配一组以上的大括号,请执行以下操作:

K='Product will be hot (These cooking instructions were. developed using an 100 watt microwave oven). For lower wattage ovens (up to an additional 2 minutes. cooking time may be required).'

>>> re.findall('\(.*?\.+.*?\)', K)
['(These cooking instructions were. developed using an 100 watt microwave oven)', '(up to an additional 2 minutes. cooking time may be required)']

>>> re.sub('\(.*?\.+.*?\)', '', K)
'Product will be hot . For lower wattage ovens .'

答案 2 :(得分:1)

请注意,如果括号中有两个以上的句点,则不会进行替换;而且,不会合并两个括号中的节,从而消除了它们之间的文本:

>>> re.sub(r'\(([^.(]*\.){1,2}[^.()]*\)',"",K)
'Product will be hot.'

如果您还想删除两个以上带有括号的部分,则可以简单地将{1,2}替换为+

>>> re.sub(r'\(([^.(]*\.)+[^.()]*\)',"",K)
相关问题