错误:Python脚本中带有正则表达式的转义错误

错误:Python脚本中带有正则表达式的转义错误,python,regex,Python,Regex,我试图在列表之间进行搜索,并在匹配和不匹配时返回值 import re array = ['brasil','argentina','chile','canada'] array2 = ['brasil.sao_paulo','chile','argentina'] for x,y in zip(array,array2): if re.search('\\{}\\b'.format(x), y, re.IGNORECASE): print("Match: {}".format(

我试图在列表之间进行搜索,并在匹配和不匹配时返回值

import re

array = ['brasil','argentina','chile','canada']
array2 = ['brasil.sao_paulo','chile','argentina']

for x,y in zip(array,array2):
  if re.search('\\{}\\b'.format(x), y, re.IGNORECASE):
    print("Match: {}".format(x))
  else:
    print("Not match: {}".format(y))
输出:

不匹配:巴西圣保罗
非对手:智利
回溯(最近一次呼叫最后一次):
文件“main.py”,第7行,在
如果重新搜索(“\\{}\\b.”格式(x),y,re.IGNORECASE):
搜索中的文件“/usr/local/lib/python3.7/re.py”,第183行
re.error:位置0处的错误转义\c
期望输出:

匹配:巴西
比赛:阿根廷
比赛:智利
非对手:加拿大

如果你
zip
,你只能得到成对匹配。考虑到搜索的性质,您只需将草堆连接到一个空格分隔的字符串中,然后交替将针连接到一个模式中,然后让
findall
chug离开:

>>> import re
>>> needles = ['brasil', 'argentina', 'chile', 'canada']
>>> haystack = ['brasil.sao_paulo', 'chile', 'argentina']
>>> re.findall(r"\b%s\b" % "|".join(needles), " ".join(haystack), re.I)
['brasil', 'chile', 'argentina']

原始正则表达式中
\\
背后的意图不清楚,因此我假设您希望
\b
位于模式的两侧。

如果您
zip
,则只能获得成对匹配。考虑到搜索的性质,您只需将草堆连接到一个空格分隔的字符串中,然后交替将针连接到一个模式中,然后让
findall
chug离开:

>>> import re
>>> needles = ['brasil', 'argentina', 'chile', 'canada']
>>> haystack = ['brasil.sao_paulo', 'chile', 'argentina']
>>> re.findall(r"\b%s\b" % "|".join(needles), " ".join(haystack), re.I)
['brasil', 'chile', 'argentina']

原始正则表达式中
\\
背后的意图不清楚,因此我假设您希望在模式的两侧都使用
\b

如果我理解正确,这里不需要正则表达式

group_1 = ['brasil','argentina','chile','canada']
group_2 = ['brasil.sao_paulo','chile','argentina']

for x in group_1:
    # For group 2 only, this picks out the part of the string that appears before the first ".".
  if x in [y.split('.')[0] for y in group_2]:
    print("Match: {}".format(x))
  else:
    print("Not match: {}".format(x))
返回

Match: brasil
Match: argentina
Match: chile
Not match: canada

如果我理解正确,这里不需要正则表达式

group_1 = ['brasil','argentina','chile','canada']
group_2 = ['brasil.sao_paulo','chile','argentina']

for x in group_1:
    # For group 2 only, this picks out the part of the string that appears before the first ".".
  if x in [y.split('.')[0] for y in group_2]:
    print("Match: {}".format(x))
  else:
    print("Not match: {}".format(x))
返回

Match: brasil
Match: argentina
Match: chile
Not match: canada

使用
any
方法的简单解决方案:

array = ['brasil', 'argentina', 'chile', 'canada']
array2 = ['brasil.sao_paulo', 'chile', 'argentina']

for x in array:
    if any(x.casefold() in y.casefold() for y in array2):
        print("Match:", x)
    else:
        print("Not match:", x)


编辑:使用
casefold()
使其不区分大小写。

使用
any
方法的简单解决方案:

array = ['brasil', 'argentina', 'chile', 'canada']
array2 = ['brasil.sao_paulo', 'chile', 'argentina']

for x in array:
    if any(x.casefold() in y.casefold() for y in array2):
        print("Match:", x)
    else:
        print("Not match:", x)


编辑:使用
casefold()
使其不区分大小写。

失败的正则表达式是
\chile\b
。我想这不是您想要搜索的。我希望它能在不考虑数组顺序的情况下进行查找示例:array 1->Line1==Array2->All lines初始“\\`”的用途是什么?我对regex不熟悉,只是想尝试一下,建议不要这样做?失败的regex是
\chile\b
。我想这不是您想要搜索的内容。我希望无论数组顺序如何都能找到它示例:array 1->Line1==Array2->All lines初始“\\`”的用途是什么?我对regex不太熟悉,只是想尝试一下,建议其他方法?