使用 re.match 匹配字符串不起作用 [英] Matching strings with re.match doesn't work

查看:32
本文介绍了使用 re.match 匹配字符串不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自这个链接我使用以下代码:

From this link I used the following code:

my_other_string = 'the_boat_has_sunk'
my_list = ['car', 'boat', 'truck']
my_list = re.compile(r'\b(?:%s)\b' % '|'.join(my_list))
if re.match(my_list, my_other_string):
    print('yay')

然而它不起作用.我尝试在重新编译后打印 my_list 并打印:

However it doesn't work. I tried printing my_list after re.compile and it prints this:

re.compile('\\b(?:car|boot|truck)\\b')

我做错了什么?

推荐答案

这不是用下划线连接单词的常规句子.由于您只是在检查单词是否存在,您可以删除 \b (因为它在单词边界上匹配并且 _ 是一个单词字符!)或添加替代方案:

This is not a regular sentence where words are joined with an underscore. Since you are just checking if the word is present, you may either remove \b (as it is matching on a word boundary and _ is a word character!) or add alternatives:

import re
my_other_string = 'the_boat_has_sunk'
my_list = ['car', 'boat', 'truck']
my_list = re.compile(r'(?:\b|_)(?:%s)(?=\b|_)' % '|'.join(my_list))
if re.search(my_list, my_other_string):
    print('yay')

参见 IDEONE 演示

编辑:

既然你说如果列表中的一个词在字符串中,它必须是真的,不仅作为一个单独的词,而且它不匹配,例如船屋在字符串中,我建议先用空格替换非单词字符和 _,然后使用 \b 的正则表达式:

Since you say it has to be true if one of the words in the list is in the string, not only as a separate word, but it musn't match if for example boathouse is in the string, I suggest first replacing non-word characters and _ with space, and then using the regex you had with \b:

import re
my_other_string = 'the_boathouse_has_sunk'
my_list = ['car', 'boat', 'truck']
my_other_string = re.sub(r'[\W_]', ' ', my_other_string)
my_list = re.compile(r'\b(?:%s)\b' % '|'.join(my_list))
if re.search(my_list, my_other_string):
    print('yay')

这不会打印yay,但如果你删除house,它会.

This will not print yay, but if you remove house, it will.

参见 IDEONE 演示 2

这篇关于使用 re.match 匹配字符串不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆