python编写正则表达式匹配单词_Python正则表达式匹配整个单词

54584d560001571a02200220-100-100.jpg

守候你守候我

我认为,通过给出的答案,OP所期望的行为并没有完全实现。具体来说,布尔值的期望输出没有完成。给出的答案做帮助说明这一概念,我认为他们是优秀的。也许我可以说明我的意思,我认为OP使用了下面的例子。给出的绳子是,a = "this is a sample"“任择议定书”接着说,我想匹配整个单词-例如匹配"hi"应该回来False自"hi"不是一个词.。据我所知,引用的是搜索标记,"hi"正如世界上所发现的,"this"..如果有人搜索字符串,a为单词 "hi",他们应该接受False作为回应。行动还在继续,..和"is"应该回来True因为左边和右边没有阿尔法字符。在本例中,引用是对搜索令牌的引用。"is"就像在单词中找到的那样"is"..我希望这有助于澄清为什么我们使用单词边界。其他答案的行为是“不返回一个单词,除非这个词是自己找到的-而不是在其他单词的内部。”“词界”速记字符类做得很好。只有这个词"is"到目前为止已经在例子中使用过了。我认为这些答案是正确的,但我认为有更多的问题的根本意义,需要解决。为了理解这个概念,应该注意其他搜索字符串的行为。换句话说,我们需要泛化@Georg给出的(极好的)答案re.match(r"\bis\b", your_string)同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.pyimport redef 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_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>>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值