我很难找到适用于以下场景的正确正则表达式:
假设有:
a = "this is a sample"
我希望匹配整个单词 - 例如,匹配"hi"
应该返回False,因为"hi"
不是一个单词,而"is"
应该返回True,因为左右两侧没有字母字符。
re.search(r'\bis\b', your_string)
来自文档:
\b
匹配一个空字符串,但只在单词的开头或结尾。
请注意,re
模块将“单词”简单定义为“字母数字或下划线字符序列”,其中“字母数字”取决于区域设置或Unicode选项。
还要注意,如果没有使用原始字符串前缀,\b
将被视为“退格”而不是正则表达式中的单词边界。
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']
以下是re.search()
文档中的内容:
\b
匹配空字符串,但只能在单词的开头或结尾处匹配。...
例如,
r'\bfoo\b'
可以匹配'foo'
、'foo.'
、'(foo)'
、'bar foo baz'
,但不能匹配'foobar'
或'foo3'
。
给定字符串为,
a = "this is a sample"
然后OP陈述道,
我想匹配整个单词,例如匹配
"hi"
应返回False
,因为"hi"
不是一个单词...
据我所知,引用是搜索标记 "hi"
在单词 "this"
中找到的情况。如果有人在字符串 a
中搜索单词 "hi"
,他们应该收到 False
作为响应。
OP继续说,
... 而
"is"
应返回True
,因为左侧和右侧都没��字母字符。
在这种情况下,引用是搜索标记 "is"
在单词 "is"
中找到的情况。我希望这有助于澄清为什么我们使用单词边界的事情。其他答案的行为是“除非该单词单独出现,否则不返回单词 - 不在其他单词内部。” “单词边界” 简写字符类 很好地完成了这项工作。
到目前为止,只使用了单词 "is"
的示例。我认为这些答案是正确的,但我认为还需要处理更多问题的基本含义。应注意其他搜索字符串的行为以了解该概念。换句话说,我们需要通过使用re.match(r"\bis\b", your_string)
来 概括 @georg的(优秀)答案。同样的r"\bis\b"
概念也用于@OmPrakash的答案中,他通过展示开始泛化讨论来完成这一点。
>>> y="this isis a sample." >>> regex=re.compile(r"\bis\b") # For ignore case: re.compile(r"\bis\b", 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"\bis\b", a)
<_sre.SRE_Match object; span=(5, 7), match='is'>
>>> re.search(r"\bhi\b", 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"\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)
以下是一个简单的演示。在保存了文件find_only_whole_word.py
的同一目录中运行Python解释器。
>>> 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
>>>
>>> find_only_whole_word("another sentence", "To show this, I will use another sentence.")
返回 True
。这可能是期望的行为,因此我将保留我的答案。 - bballdave025findall
方法来编写一行代码:>>> len(re.findall(r"\bhi\b", "This IS a sample.", flags=re.IGNORECASE))
返回 False
。任何其他所需的字符串都可以放在\b
之间。 - bballdave025 word="is"
srchedStr="this is a sample"
if srchedStr.find(" "+word+" ") >=0 or \
srchedStr.endswith(" "+word):
<do stuff>
条件语句的第一部分查找两侧都有空格的文本,而第二部分则捕获字符串结尾的情况。请注意,endwith
是布尔型,而 find
返回一个整数。
test!
并不是我所知道的任何一个定义中的词。有趣的是,它对于缩略词是有效的:re.search(r"\bisn't\b", "it isn't bad")
会返回匹配。 - Jeff Learmanre.search(r"\bisn'\b", "it isn' bad")
没有匹配结果。这不是撇号特殊,而是位置特殊。单词(模式)可以在内部具有标点符号,但不能在开头或结尾。test!a
可以匹配某些内容,但test!
则无法。 - Jeff Learman\x08
而不是\b
? - Peter.kbis
保存在一个变量中,如何使其工作?foo =“bis” re.search("\ b"+ foo +"\ b", your_string)
无法正常工作。 - Arteezy