Python 字符串完全匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4173787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:37:22  来源:igfitidea点击:

String exact match

pythonstring

提问by Lalit Chattar

I have a string in which the word "LOCAL" occurs many times. I used the find()function to search for this word but it returns another word "Locally" as well. How can I match the word "local" exactly?

我有一个字符串,其中多次出现“LOCAL”这个词。我使用该find()函数来搜索这个词,但它也返回另一个词“本地”。我怎样才能准确匹配“本地”这个词?

采纳答案by Vincent Savard

For this kind of thing, regexps are very useful :

对于这种事情,正则表达式非常有用:

import re

print(re.findall('\blocal\b', "Hello, locally local test local."))
// ['local', 'local']

\b means word boundary, basically. Can be space, punctuation, etc.

\b 基本上是指单词边界。可以是空格、标点符号等。

Edit for comment :

编辑评论:

print(re.sub('\blocal\b', '*****', "Hello, LOCAL locally local test local.", flags=re.IGNORECASE))
// Hello, ***** locally ***** test *****.

You can remove flags=re.IGNORECASE if you don't want to ignore the case, obviously.

显然,如果您不想忽略这种情况,您可以删除 flags=re.IGNORECASE。

回答by Danny Navarro

Look for ' local '? Notice that Python is case sensitive.

寻找“本地”?请注意,Python 区分大小写。

回答by OmnipotentEntity

Do a regular expression search for \blocal\b

对 \blocal\b 进行正则表达式搜索

\b is a "word boundry" it can include beginnings of lines, ends of lines, punctuation, etc.

\b 是一个“单词边界”,它可以包括行首、行尾、标点等。

You can also search case insensitively.

您还可以不区分大小写地搜索。

回答by ssegvic

You could use regular expressions to constrain the matches to occur at the word boundary, like this:

您可以使用正则表达式将匹配项限制在单词边界处,如下所示:

import re
p = re.compile(r'\blocal\b')
p.search("locally") # no match
p.search("local") # match
p.findall("rty local local k") # returns ['local', 'local']

回答by Guray Celik

Below you can use simple function.

下面你可以使用简单的功能。

def find_word(text, search):

   result = re.findall('\b'+search+'\b', text, flags=re.IGNORECASE)
   if len(result)>0:
      return True
   else:
      return False

Using:

使用:

text = "Hello, LOCAL locally local test local."
search = "local"
if find_word(text, search):
  print "i Got it..."
else:
  print ":("

回答by Toshiro

line1 = "This guy is local"
line2 = "He lives locally"

if "local" in line1.split():
    print "Local in line1"
if "local" in line2.split():
    print "Local in line2"

Only line1 will match.

只有 line1 会匹配。

回答by Raphael

Using Pyparsing:

使用 Pyparsing:

import pyparsing as pp

def search_exact_word_in_string(phrase, text):

    rule = pp.ZeroOrMore(pp.Keyword(phrase))  # pp.Keyword() is case sensitive
    for t, s, e in rule.scanString(text):
      if t:
        return t
    return False

text = "Local locally locale"
search = "Local"
print(search_exact_word_in_string(search, text))

Which Yields:

哪个产量:

['Local']

['Local']