Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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/8/python-3.x/15.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
Regex 正则表达式返回括号之间有一个或多个句点的文本_Regex_Python 3.x - Fatal编程技术网

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

Regex 正则表达式返回括号之间有一个或多个句点的文本,regex,python-3.x,Regex,Python 3.x,我有一个文本,在括号之间有一个或多个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'\((.*?)

我有一个文本,在括号之间有一个或多个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) 


但它们都不会返回文本

您可以使用表达式:

(?<=\()[^()]*(?=\))
印刷品:

['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']
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))

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 .'

这需要注意的是,如果括号中有两个以上的句点,则不会进行替换,并且不会合并两个括号中的部分,从而消除它们之间的文本:

>>> re.sub(r'\(([^.(]*\.){1,2}[^.()]*\)',"",K)
'Product will be hot.'
如果您还想删除带有两个以上句点的圆括号部分,只需将{1,2}替换为+:


你想写一个或两个句点还是一个或多个句点?我有一些文本包含两个或多个带大括号的字符串和两个或多个带句点的句子。例如:“产品将根据烹饪说明加热。”。使用。100瓦的微波炉。对于功率较低的烤箱,最多可延长2分钟。所以,我想剥掉盒子里所有的绳子braces@PavanYeddanapudi提供的任何解决方案对您有效吗?如果是这样,你可以接受/向上投票说的答案,看看怎么做。如果我有一套以上的大括号怎么办?例如:K=”产品将是热的,这些烹饪说明都是热的。使用100瓦微波炉开发。对于低瓦数烤箱,最多可延长2分钟。烹饪时间可能需要。're.sub.*?\.+.*',K也消除了低瓦数烤箱的中间串。但我确实需要那篇文章。这很有效。我还有一个问题。如何反向处理正则表达式?或者我想消除字符前面的字符串?示例产品将是热的:这些烹饪说明是我之前想消除的字符串:哪个产品是热的?如果我有一套以上的牙套怎么办?例如:K=”产品将是热的,这些烹饪说明都是热的。使用100瓦微波炉开发。对于低瓦数烤箱,最多可延长2分钟。可能需要烹饪时间。但这并没有给我我想要的结果,但萨库尔的上述想法奏效了。无论如何,我还有一个问题。如何反向处理正则表达式?或者我想消除字符前面的字符串?示例产品将是热的:这些烹饪说明是我之前开发的,我想消除字符串:哪个是热的产品?我的解决方案符合您的要求,它匹配两个字符串中的内容。检查链接。在这种情况下,您可以使用:K=产品将是热的:这些烹饪说明是由printre.subr'[^:]*?=:',K制定的,如果您想保留:也可以。
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 .'
>>> re.sub(r'\(([^.(]*\.){1,2}[^.()]*\)',"",K)
'Product will be hot.'
>>> re.sub(r'\(([^.(]*\.)+[^.()]*\)',"",K)