如何在字符串中搜索单词(完全匹配)? [英] How to search for a word (exact match) within a string?

查看:77
本文介绍了如何在字符串中搜索单词(完全匹配)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试子字符串搜索

>>>str1 = 'this'>>>str2 = '研究这个'>>>str3 = '研究这个'>>>"[^a-z]"+str1+"[^a-z]" 在 str2错误的>>>"[^a-z]"+str1+"[^a-z]" in str3错误的

在查看 str3 时,我想要 True.我做错了什么?

解决方案

你想要 Python 的 re 模块:

<预><代码>>>>进口重新>>>regex = re.compile(r"\sthis\s") # \s 是空格>>># 或者>>>regex = re.compile(r"\Wthis\W")>>># \w 是单词字符 ([a-zA-Z0-9_]),\W 不是单词字符>>>str2 = '研究这个'>>>str3 = '研究这个'>>>布尔(正则表达式.搜索(str2))错误的>>>regex.search(str3)<_sre.SRE_Match 对象在 0x10044e8b8>>>>布尔(正则表达式.搜索(str3))真的

我有一种预感,您实际上是在寻找单词this",而不是this",并且周围有非单词字符.在这种情况下,您应该使用单词边界转义序列 \b.

I am trying to substring search

>>>str1 = 'this'
>>>str2 = 'researching this'
>>>str3 = 'researching this '

>>>"[^a-z]"+str1+"[^a-z]" in str2
False

>>>"[^a-z]"+str1+"[^a-z]" in str3
False

I wanted to True when looking in str3. what am I doing wrong?

解决方案

You want Python's re module:

>>> import re
>>> regex = re.compile(r"\sthis\s") # \s is whitespace
>>> # OR
>>> regex = re.compile(r"\Wthis\W")
>>> # \w is a word character ([a-zA-Z0-9_]), \W is anything but a word character
>>> str2 = 'researching this'
>>> str3 = 'researching this '
>>> bool(regex.search(str2))
False
>>> regex.search(str3)
<_sre.SRE_Match object at 0x10044e8b8>
>>> bool(regex.search(str3))
True

I have a hunch you're actually looking for the word "this", not "this" with non-word characters around it. In that case, you should be using the word boundary escape sequence \b.

这篇关于如何在字符串中搜索单词(完全匹配)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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