社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

错误:带有regex的Python脚本中的错误转义

Luis Henrique • 5 年前 • 1453 次点击  

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

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))

输出:

Not match: brasil.sao_paulo
Not match: chile
Traceback (most recent call last):
  File "main.py", line 7, in <module>
    if re.search('\\{}\\b'.format(x), y, re.IGNORECASE):
  File "/usr/local/lib/python3.7/re.py", line 183, in search
re.error: bad escape \c at position 0

期望输出:

Match: brasil
Match: argentina
Match: chile
Not match: canada
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51504
 
1453 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Michael Chatiskatzi
Reply   •   1 楼
Michael Chatiskatzi    5 年前

一个简单的解决方案 any 方法:

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

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

Try it online!

ggorlen
Reply   •   2 楼
ggorlen    5 年前

如果你 zip ,你只能得到成对的火柴。考虑到搜索的性质,您可以将大海捞针连接成一个空格分隔的字符串,并将针连接成一个交替的模式,然后 findall 突然离开:

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

背后的意图 \\ 在最初的regex中是不清楚的,所以我想你想要 \b 在图案的两边。

David Remus
Reply   •   3 楼
David Remus    5 年前

如果我理解正确,这里不需要regex。

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