Python 正则表达式不捕获单个字符串

Python Regex not capturing single strings

bigger_list_of_names = ['Jim', 'Bob', 'Fred', 'Cam', 'Reagan','Alejandro','Dee','Rana','Denisha','Nicolasa','Annett','Catrina','Louvenia','Emmanuel','Dina','Jasmine','Shirl','Jene','Leona','Lise','Dodie','Kanesha','Carmela','Yuette',]
name_list = ['Jim', 'Bob', 'Fred', 'Cam']
search_people = re.compile(r'\b({})\b'.format(r'|'.join(name_list)), re.IGNORECASE)
print(search_people)

for names in bigger_list_of_names:
    found_them = search_people.search(names, re.IGNORECASE | re.X)
    print(names)
    if found_them:
        print('I found this person: {}'.format(found_them.group()))
    else:
        print('Did not find them')

我遇到的问题是正则表达式根本找不到名称并一直点击 else: re.searchre.findallre.findre.matchre.fullmatch等都试过了,都是returnNone。它找到任何东西的唯一方法是如果我使用 re.finditer 但这不允许我使用 .group().

re.compile 的输出是 re.compile('\b(Jim|Bob|Fred|Cam)\b', re.IGNORECASE)

我在 https://regex101.com/ () 上测试过它,它看起来可以正常工作,但在 python.

中没有

这是我的控制台输出:

我错过了什么吗?

编译后的正则表达式的第二个参数是字符串中开始搜索的位置,而不是与正则表达式一起使用的标志(第三个也是可选参数,是搜索的结束位置)。有关详细信息,请参阅 Regular expression objects 的文档。
如果要指定不区分大小写的搜索,请将 re.IGNORECASE 传递给 re.compile。对于此正则表达式,不需要 re.X

您要执行的操作不需要正则表达式搜索。您可以实现相同的效果。

search_result = []
targets = set(names_list)
for name in set(bigger_list_of_names):
    if name in targets:
        search_result.append(name)
        print(f'Found name: {name}')
    else:
        print(f'Did not find name: {name}')
print(search_result)

使用列表理解

的较短版本
search_result = [name for name in set(bigger_list_of_names) if name in targets]

我来晚了一点,但我之前遇到过同样的问题。看起来你正在使用 pycharm,如果你检查自动完成的东西(如果你没有关闭它)它会说:

pattern.search(self, string, pos, endpos)

您需要在 re.compile 部分添加标志,而不是在 .search() 部分添加标志。因为 re.compile() 实际上接受了标志。

pycharm 自动完成的样子:

re.compile(pattern, flags)

所以它看起来有点像这样:

search_people = re.compile(r'\b({})\b'.format(r'|'.join(name_list)), re.IGNORECASE | re.X)

for names in bigger_list_of_names:
    found_them = search_people.search(names)
    print(names)
    if found_them:
        print('I found this person: {}'.format(found_them.group()))
    else:
        print('Did not find them')