简体   繁体   English

在python中使用“in”匹配整个单词

[英]Matching whole words using "in" in python

I've been searching around for some time for this, but have still not found an answer, maybe its got some thing to do with regular expressions, but i think there should be a simple answer that I am missing here.我已经为此寻找了一段时间,但仍然没有找到答案,也许它与正则表达式有关,但我认为应该有一个我在这里遗漏的简单答案。 It seems very trivial to me ... here goes:这对我来说似乎很微不足道......这里是:

On the python interpreter I get:在 python 解释器上,我得到:

"abc" in "abc123" 

as True.为真。

I want it a command that returns a False.我想要一个返回 False 的命令。 I want the entire word to be matched.我希望整个单词都匹配。

Thanks!谢谢!

in isn't how it's done. in不是它是如何完成的。

>>> re.search(r'\babc\b', 'abc123')
>>> re.search(r'\babc\b', 'abc 123')
<_sre.SRE_Match object at 0x1146780>

If you want to do a plain match of just one word, use ==:如果你想只匹配一个单词,使用 ==:

'abc' == 'abc123' # false

If you're doing 'abc' in ['cde','fdabc','abc123'] , that returns False anyway:如果您'abc' in ['cde','fdabc','abc123']无论如何都会返回 False :

'abc' in ['cde','fdabc','abc123'] # False

The reason 'abc' in 'abc123' returns true, from the docs : 'abc' in 'abc123'的原因返回 true,来自文档

For the Unicode and string types, x in y is true if and only if x is a substring of y .对于 Unicode 和字符串类型,当且仅当x是 y 的子字符串y x in y为真。 An equivalent test is y.find(x) != -1 .等效测试是y.find(x) != -1

So for comparing against a single string, use '==', and if comparing in a collection of strings, in can be used (you could also do 'abc' in ['abc123'] - since the behaviour of in works as your intuition imagines when y is a list or collection of sorts.因此,要与单个字符串进行比较,请使用 '==',如果在字符串集合中进行比较,则可以使用in (您也可以'abc' in ['abc123'] - 因为in的行为就像您的直觉想象当y是一个列表或排序集合时。

I might not understand your question, but it seems like what you want is "abc123" == "abc" .我可能不明白你的问题,但似乎你想要的是"abc123" == "abc" This returns False , whereas "abc123" == "abc123" returns True .这将返回False ,而"abc123" == "abc123"返回True

Perhaps what you are looking for is matching on whole words but splitting on whitespace?也许您正在寻找的是匹配整个单词但拆分空格? That is, "abc" does not match "abc123" , but it does match "abc def" ?也就是说, "abc"不匹配"abc123" ,但它匹配"abc def" If that is the case, you want something like this:如果是这样的话,你想要这样的东西:

def word_in (word, phrase):
    return word in phrase.split()

word_in("abc", "abc123") # False
word_in("abc", "abc def") # True

In my case, I used a small trick.就我而言,我使用了一个小技巧。 The whole word should be surrounded with spaces.整个单词应该用空格包围。 So, if I want to find word like " Kill ", I will search for " Kill ".所以,如果我想找到像“ Kill ”这样的词,我会搜索“ Kill ”。 In this case, it wont match with word like " Skill "在这种情况下,它不会与“技能”之类的词匹配

' kill ' in myString

Perhaps a wrong shot but it can be done in a more simple way.也许是一个错误的镜头,但可以以更简单的方式完成。

def word_in(needle,haystack,case_sensitive=True):
    if needle + ' ' in haystack or ' ' + needle + ' ' in haystack or needle + ' ' in haystack:
        return True

    return False

print word_in('abc','abc123')    
print word_in('abc','abc 123')

First example produces False, the other True第一个例子产生假,另一个真

You can try to use find method and compare the result to -1:您可以尝试使用find方法并将结果与​​ -1 进行比较:

>>> a = "abc123"
>>> a.find("abc")
0
>>> a.find("bcd")
-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM