Python正则表达式匹配整词

Python正则表达式匹配整词,python,regex,Python,Regex,在以下场景中,我很难找到正确的正则表达式: 让我们说: a = "this is a sample" 我想匹配整个单词-例如match“hi”应该返回False,因为“hi”不是单词,“is”应该返回True,因为左右两侧没有字母字符。试试看 re.search(r'\bis\b', your_string) 发件人: \b匹配空字符串,但仅在单词的开头或结尾处匹配 请注意,re模块将“单词”简单定义为“字母数字或下划线字符序列”,其中“字母数字”取决于区域设置或unicode选项 还请注意

在以下场景中,我很难找到正确的正则表达式:

让我们说:

a = "this is a sample"
我想匹配整个单词-例如match
“hi”
应该返回False,因为
“hi”
不是单词,
“is”
应该返回True,因为左右两侧没有字母字符。

试试看

re.search(r'\bis\b', your_string)
发件人:

\b匹配空字符串,但仅在单词的开头或结尾处匹配

请注意,
re
模块将“单词”简单定义为“字母数字或下划线字符序列”,其中“字母数字”取决于区域设置或unicode选项


还请注意,如果没有原始字符串前缀,
\b
被视为“backspace”,而不是regex单词边界。

regex的问题是,如果要在另一个字符串中搜索的字符串包含regex字符,它会变得复杂。任何带括号的字符串都将失败

此代码将找到一个单词

 word="is"
    srchedStr="this is a sample"
    if srchedStr.find(" "+word+" ") >=0  or \
       srchedStr.endswith(" "+word):
        <do stuff>
word=“is”
srchedStr=“这是一个示例”
如果srchedStr.find(“+word+”)>=0或\
srchedStr.endswith(“+”字):
条件搜索的第一部分搜索文本,每边各有一个空格,第二部分捕获字符串结尾的情况。请注意,endwith是布尔值,而
find
返回一个整数

请尝试在regex模块中使用“单词边界”字符类,
re

x="this is a sample"
y="this isis a sample."
regex=re.compile(r"\bis\b")  # For ignore case: re.compile(r"\bis\b", re.IGNORECASE)

regex.findall(y)
[]

regex.findall(x)
['is']
从的文档中

\b
匹配空字符串,但仅在单词的开头或结尾处匹配

例如
r'\bfoo\b'
匹配
'foo'
'foo.
'(foo)
'bar foo baz'
但不匹配
'foobar'
'foo3'


我认为OP所期望的行为并没有通过给出的答案完全实现。具体来说,布尔值的期望输出没有实现。给出的答案确实有助于说明这一概念,我认为它们非常好。也许我可以说明我的意思,我认为OP之所以使用示例,是因为以下原因

给出的字符串是

a=“这是一个示例”

OP接着说

我想匹配整个单词-例如match
“hi”
应该返回
False
,因为
“hi”
不是一个单词

据我所知,参考是指搜索标记,
“hi”
,因为它可以在单词中找到,
“this”
。如果有人在字符串
a
中搜索单词
“hi”
,他们应该会收到
False
作为响应

行动继续

。。。而
“is”
应该返回
True
,因为左侧和右侧没有字母字符

在这种情况下,参考是指搜索标记
“is”
,因为它可以在单词
“is”
中找到。我希望这有助于澄清为什么我们使用单词边界。其他答案的行为是“不要返回一个单词,除非该单词是自己找到的——而不是在其他单词的内部。”这个“单词边界”很好地完成了这项工作

到目前为止,示例中仅使用了单词
“is”
。我认为这些答案是正确的,但我认为这个问题还有更多的基本含义需要解决。应注意其他搜索字符串的行为,以理解该概念。换句话说,我们需要通过@georg使用
re.match(r“\bis\b”,您的\u字符串)来概括(优秀)答案。
相同的
r“\bis\b”
概念也用于@OmPrakash的答案中,他通过显示

让我们假设应该展示我所讨论的行为的方法被命名为

find_only_whole_word(search_string, input_string)
然后应预期以下行为

>>> a = "this is a sample"
>>> find_only_whole_word("hi", a)
False
>>> find_only_whole_word("is", a)
True
>>> 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
>>>
再一次,这是我如何理解OP的问题。通过@georg的回答,我们朝着这种行为迈出了一步,但这有点难以解释/实现。机智

>>> 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)
>>>
这可以通过以下代码实现:

#!/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)
下面是一个简单的演示。从保存文件的同一目录运行Python解释器,
find\u only\u whole\u word.py

>>> 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
>>>

谢谢,我在这个语句中添加了flags=re.ignorecase需要什么-re.search(r'\bis\b',您的_字符串)?@user2161049:
test都不是一个词。有趣的是,它确实适用于收缩:
re.search(r“\bisn't\b”,“它不坏”)
返回匹配项。奇怪的是,它不适用于终端省略:
re.search(r“\bisn'\b”,“它不坏”)
不返回匹配项。特别的不是撇号,而是位置。单词(模式)内部可以有标点符号,但不能在末尾或开头<代码>测试!一个
可以匹配一些东西,但是
测试
不能。为什么我得到的是
\x08
而不是
\b
?另外,我看到已经有了一个被接受的答案-你可能想删除你的答案,并因为投票失败而恢复声誉。@davejagoda删除答案会恢复他/她的声誉吗?@silentphoenix我相信:第一段不正确。目标字符串可以包含任意内容。如果单词出现在目标字符串的开头,则无法使用此选项。它还假设单词总是被空格包围,这在一般情况下是不正确的。我重新打开了这个问题,因为它被一篇错误的帖子作为副本关闭。请注意,如果只需要“真”的整个单词,则必须对输入进行清理
>>只查找整个单词(“另一个句子”,“为了显示这一点,我将使用另一个句子。”)
返回
True
。这可能是期望的行为,因此我保留我的答案。如果您想要使用@OsPrakesh使用的
findall
方法的一行程序:
>>len(re.findall(r”\bhi\b),“Thi”
>>> 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
>>>