<p>我认为操作人员所期望的行为并不是通过给出的答案完全实现的。具体来说,布尔值的期望输出没有完成。给出的答案有助于说明这一概念,我认为它们是优秀的。也许我可以说明我的意思,我认为OP使用了下面的例子。</p>
<p>给出的字符串是</p>
<blockquote>
<p><code>a = "this is a sample"</code> </p>
</blockquote>
<p>操作人员接着说</p>
<blockquote>
<p>I want to match whole word - for example match <code>"hi"</code> should return <code>False</code> since <code>"hi"</code> is not a word ...</p>
</blockquote>
<p>据我所知,引用是指搜索标记<code>"hi"</code>,正如在单词<code>"this"</code>中找到的那样。如果有人搜索字符串<code>a</code>来查找<strong>单词</strong><code>"hi"</code>,他们应该收到<code>False</code>作为响应。</p>
<p>手术继续</p>
<blockquote>
<p>... and <code>"is"</code> should return <code>True</code> since there is no alpha character on the left and on the right side.</p>
</blockquote>
<p>在本例中,引用是指在单词<code>"is"</code>中找到的搜索标记<code>"is"</code>。我希望这有助于澄清为什么我们使用单词边界。另一个答案的行为是“不返回一个单词,除非该单词是自己找到的,而不是在其他单词的内部。”“单词边界”<a href="https://www.regular-expressions.info/shorthand.html" rel="nofollow noreferrer">shorthand character class</a>很好地完成了这项工作。</p>
<p>到目前为止,示例中只使用了<code>"is"</code>一词。我认为这些答案是正确的,但我认为还有更多的问题的基本含义需要解决。为了理解这个概念,应该注意其他搜索字符串的行为。换句话说,我们需要用<code>re.match(r"\bis\b", your_string)</code>概括@georg的(优秀的)答案,<code>re.match(r"\bis\b", your_string)</code>同样的<code>r"\bis\b"</code>概念也用在@OmPrakash的答案中,他通过展示</p>
<blockquote>
<pre><code>>>> y="this isis a sample."
>>> regex=re.compile(r"\bis\b") # For ignore case: re.compile(r"\bis\b", re.IGNORECASE)
>>> regex.findall(y)
[]
</code></pre>
</blockquote>
<p>假设应该展示我所讨论的行为的方法是</p>
<pre><code>find_only_whole_word(search_string, input_string)
</code></pre>
<p>然后应该出现以下行为。</p>
<pre><code>>>> a = "this is a sample"
>>> find_only_whole_word("hi", a)
False
>>> find_only_whole_word("is", a)
True
</code></pre>
<p>再一次,这就是我如何理解OP的问题。根据@georg的回答,我们已经朝着这种行为迈出了一步,但这有点难以解释/实现。也就是说</p>
<pre><code>>>> import re
>>> a = "this is a sample"
>>> re.search(r"\bis\b", a)
<_sre.SRE_Match object; span=(5, 7), match='is'>
>>> re.search(r"\bhi\b", a)
>>>
</code></pre>
<p>第二个命令没有输出。@OmPrakesh给出的有用答案显示了输出,但不是<code>True</code>或<code>False</code>。</p>
<p>下面是对预期行为的更完整的抽样。</p>
<pre><code>>>> find_only_whole_word("this", a)
True
>>> find_only_whole_word("is", a)
True
>>> find_only_whole_word("a", a)
True
>>> find_only_whole_word("sample", a)
True
# Use "ample", part of the word, "sample": (s)ample
>>> find_only_whole_word("ample", a)
False
# (t)his
>>> find_only_whole_word("his", a)
False
# (sa)mpl(e)
>>> find_only_whole_word("mpl", a)
False
# Any random word
>>> find_only_whole_word("applesauce", a)
False
>>>
</code></pre>
<p>这可以通过以下代码实现:</p>
<pre><code>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#@file find_only_whole_word.py
import re
def find_only_whole_word(search_string, input_string):
# Create a raw string with word boundaries from the user's input_string
raw_search_string = r"\b" + search_string + r"\b"
match_output = re.search(raw_search_string, input_string)
##As noted by @OmPrakesh, if you want to ignore case, uncomment
##the next two lines
#match_output = re.search(raw_search_string, input_string,
# flags=re.IGNORECASE)
no_match_was_found = ( match_output is None )
if no_match_was_found:
return False
else:
return True
##endof: find_only_whole_word(search_string, input_string)
</code></pre>
<p>下面是一个简单的演示。从保存文件的目录<code>find_only_whole_word.py</code>运行Python解释器。</p>
<pre><code>>>> from find_only_whole_word import find_only_whole_word
>>> a = "this is a sample"
>>> find_only_whole_word("hi", a)
False
>>> find_only_whole_word("is", a)
True
>>> find_only_whole_word("cucumber", a)
False
# The excellent example from @OmPrakash
>>> find_only_whole_word("is", "this isis a sample")
False
>>>
</code></pre>