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/2/facebook/8.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 - Fatal编程技术网

Python 匹配不连续/中断字符串

Python 匹配不连续/中断字符串,python,regex,Python,Regex,我有一个预定义的字符串列表,我想在一个大的文本文件中匹配这些字符串。问题是,这些字符串中的许多确实存在于文本中,但被我想要保留的伪字符/HTMLXML标记打断 例如,我想匹配“联合国总部” 它可以以以下形式存在于文本中: United Nations & Headquarters United <br> Nations Headquarters United Natio<b>ns Hea</b>dquarters 正则表达式是否可以设置为以某种方式忽

我有一个预定义的字符串列表,我想在一个大的文本文件中匹配这些字符串。问题是,这些字符串中的许多确实存在于文本中,但被我想要保留的伪字符/HTMLXML标记打断

例如,我想匹配“联合国总部” 它可以以以下形式存在于文本中:

United Nations & Headquarters
United <br> Nations Headquarters
United Natio<b>ns Hea</b>dquarters
正则表达式是否可以设置为以某种方式忽略这些中断,或者解决方案是什么?

import re
import re

text = """United Nations & Headquarters
United <br> Nations Headquarters
United Natio<b>ns Hea</b>dquarters"""

s = "United Nations Headquarters"

r = re.compile(".*?".join(s))
print([v.span() for v in r.finditer(text)])
text=“”联合国和总部 联合国总部 联合国总部 s=“联合国总部” r=重新编译(“.*?.join”) 打印([v.span()表示r.finditer中的v(文本)])
键是
“*?”.join(s)
,它在
s
的每对连续字符之间插入
*?
,将其转换为正则表达式


如果您想限制允许的中断,您可能更愿意将
*?
收紧一点。

有两种解决方案可以避免灾难性的回溯并允许任何数量的中断!


解决方案A

这是最干净的解决方案,但需要模块(win二进制文件)。它使用原子分组,
(?>…)
,以避免回溯:

import regex

strExampleFile = '''United Nations & Headquarters
United <br> Nations Headquarters
United Natio<b>ns Hea</b>dquarters'''

strSearch = 'United Nations Headquarters'

strRegex = regex.sub(r'((?<!^).)',r'(?>[\s\S]*?(?=\1))\1',strSearch)
rexRegex = regex.compile(strRegex)

print([objMatch.span() for objMatch in rexRegex.finditer(strExampleFile)])
导入正则表达式
strExampleFile=''联合国和总部
联合国总部
联合国总部“
STREARCH=‘联合国总部’
strRegex=regex.sub(r'((?[\s\s]*?(?=\1))\1',strSearch)
rexRegex=regex.compile(strRegex)
打印([objMatch.span()用于rexRegex.finditer(strExampleFile)中的objMatch)


解决方案B

如果您既没有安装也不想安装模块,则可以使用re模拟原子分组。但是,搜索字符串现在限制为最多100个字符:

import re

strExampleFile = '''United Nations & Headquarters
United <br> Nations Headquarters
United Natio<b>ns Hea</b>dquarters'''

strSearch = 'United Nations Headquarters'

strRegex = re.sub(r'((?<!^).)',r'(?=([\s\S]*?(?=\1)))\\##\1',strSearch)

for numBackReference in range(1,len(strSearch)) :
    strRegex = strRegex.replace("##", str(numBackReference),1)

rexRegex = re.compile(strRegex)

print([objMatch.span() for objMatch in rexRegex.finditer(strExampleFile)])
重新导入
strExampleFile=''联合国和总部
联合国总部
联合国总部“
STREARCH=‘联合国总部’

stregex=re.sub(r'((?该文件是一个实际的html/xml/something文件吗?或者只是有时候里面有xml序列吗?它是word文档的xml文件,我寻找的字符串主要是被xml标记打断的。我认为不太清楚什么是可接受的匹配。如果它可以被打断任意多次,那么NPE非常宽容的回答就行了,而且这是一个可以接受的匹配:“联合导航员采取了行动,向发展中国家提供了大量美味的优质啤酒。”@femtoRgon你说得对,让我们关注被xml标记打断的字符串在这种情况下,而不是
*?
使用类似
(?:]+>)?
。这只会消除一个标记。也许这有点太宽容了,但也足够公平:Pinteresting解决方案,但这并不奇怪。
*?
在每个字符之间都会导致大量的回溯!你真的,真的想把它收紧很多。所以如何收紧这些,有些字符是只是连续的,当有中断时,它可以是数百个字符apart@hmghaly-我们需要确切地知道中断可能是什么。然后我们也许能够设计一个合适的正则表达式。
import re

strExampleFile = '''United Nations & Headquarters
United <br> Nations Headquarters
United Natio<b>ns Hea</b>dquarters'''

strSearch = 'United Nations Headquarters'

strRegex = re.sub(r'((?<!^).)',r'(?=([\s\S]*?(?=\1)))\\##\1',strSearch)

for numBackReference in range(1,len(strSearch)) :
    strRegex = strRegex.replace("##", str(numBackReference),1)

rexRegex = re.compile(strRegex)

print([objMatch.span() for objMatch in rexRegex.finditer(strExampleFile)])