如何用正则表达式匹配整个单词?
- 2024-11-25 08:49:00
- admin 原创
- 182
问题描述:
我无法找到适合以下场景的正确正则表达式:
可以说:
a = "this is a sample"
我想匹配整个单词 - 例如匹配"hi"
应该返回 False,因为"hi"
它不是一个单词,并且"is"
应该返回 True,因为左边和右边没有字母字符。
解决方案 1:
尝试
re.search(r'is', your_string)
来自文档:
匹配空字符串,但仅位于单词的开头或结尾。
请注意,该re
模块使用“单词”的简单定义作为“字母数字或下划线字符的序列”,其中“字母数字”取决于语言环境或unicode选项。
还要注意,如果没有原始字符串前缀,则被视为“退格键”而不是正则表达式字边界。
解决方案 2:
尝试使用正则表达式模块中的“单词边界”字符类re
:
x="this is a sample"
y="this isis a sample."
regex=re.compile(r"is") # For ignore case: re.compile(r"is", re.IGNORECASE)
regex.findall(y)
[]
regex.findall(x)
['is']
来自 的文档re.search()
。
匹配空字符串,但仅限于单词的开头或结尾
...
例如
r'foo'
匹配'foo'
,'foo.'
,但不匹配'(foo)'
或'bar foo baz'
`'foobar'`'foo3'
解决方案 3:
我认为,使用给出的答案,原帖作者所期望的行为并没有完全实现。具体来说,布尔值的期望输出没有实现。给出的答案确实有助于说明这个概念,我认为它们很棒。也许我可以通过说明我认为原帖作者使用所用示例是因为以下内容来说明我的意思。
给出的字符串是,
a = "this is a sample"
OP 随后表示,
我想匹配整个单词-例如匹配
"hi"
应该返回False
因为"hi"
它不是一个单词...
据我了解,该引用指的是搜索标记,"hi"
因为它是在单词中找到的。如果有人在字符串中"this"
搜索单词,他们应该会收到响应。a
"hi"
`False`
OP 继续说道,
...并且
"is"
应该返回True
,因为左侧和右侧没有字母字符。
"is"
在这种情况下,引用的是单词 中的搜索标记"is"
。我希望这有助于澄清我们使用单词边界的原因。其他答案的行为是“除非该单词单独存在(不在其他单词内),否则不返回该单词。”“单词边界”简写字符类可以很好地完成这项工作。
到目前为止,只有这个词"is"
在示例中使用过。我认为这些答案是正确的,但我认为还有更多问题的基本含义需要解决。应该注意其他搜索字符串的行为以理解这个概念。换句话说,我们需要使用 @georg 的答案来概括(优秀)答案。@OmPrakash 的答案也使用了re.match(r"is", your_string)
相同的r"is"
概念,他通过展示来开始概括讨论
>>> y="this isis a sample." >>> regex=re.compile(r"is") # For ignore case: re.compile(r"is", re.IGNORECASE) >>> regex.findall(y) []
假设应该表现出我所讨论的行为的方法名为
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
再次重申,这就是我对 OP 问题的理解。@georg 的回答让我们朝着这个行为迈进了一步,但解释/实施起来有点困难。也就是说
>>> import re
>>> a = "this is a sample"
>>> re.search(r"is", a)
<_sre.SRE_Match object; span=(5, 7), match='is'>
>>> re.search(r"hi", a)
>>>
第二个命令没有输出。@OmPrakesh 提供的有用答案显示了输出,但没有显示True
或False
。
以下是预期行为的更完整示例。
>>> 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
>>>
这可以通过以下代码实现:
#!/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"" + search_string + r""
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_only_whole_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
>>>
解决方案 4:
正则表达式的问题在于,如果您想在另一个字符串中搜索的字符串包含正则表达式字符,则会变得复杂。任何带有括号的字符串都会失败。
此代码将找到一个单词
word="is"
srchedStr="this is a sample"
if srchedStr.find(" "+word+" ") >=0 or \n srchedStr.endswith(" "+word):
<do stuff>
条件的第一部分搜索两边都有空格的文本,第二部分捕获字符串结尾的情况。请注意,endwith 是布尔值,而find
返回一个整数