正则表达式在字符串中查找最后一个单词(Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19454412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:46:59  来源:igfitidea点击:

Regex to find last word in a string (Python)

pythonregex

提问by rajaditya_m

I am trying to write a simple regex that finds if the last word in the string is a specific one.

我正在尝试编写一个简单的正则表达式,用于查找字符串中的最后一个单词是否是特定单词。

I wrote something like this "(\W|^)dog$". (Check if last word in the sentence is dog)

我写了这样的东西"(\W|^)dog$"。(检查句子的最后一个词是否是狗)

This regex is correct but in python it is returning nothing when i type something like "I like dog".

这个正则表达式是正确的,但在 python 中,当我输入类似"I like dog".

I tested this in the Rubular regex editor and it seems to work.

我在 Rubular regex 编辑器中对此进行了测试,它似乎有效。

Am I doing something wrong ?

难道我做错了什么 ?

EDIT : Adding my simple code

编辑:添加我的简单代码

import re
pm = re.compile("(\W|^)dog$")
has = pm.match("i love dog")
print(has)

采纳答案by Rohit Jain

You don't need to regex here. Simple split will do the job:

您不需要在这里使用正则表达式。简单的拆分将完成这项工作:

>>> s = "I like dog"
>>> s.rsplit(None, 1)[-1] == 'dog'
True

Since you need the last word only, str.rsplitcan be used to start splitting from end, and passing 1as 2nd argument, will only perform split only once. Then get the last element of the returned list.

由于您只需要最后一个单词,str.rsplit可用于从末尾开始拆分,并1作为第二个参数传递,只会执行一次拆分。然后获取返回列表的最后一个元素。



As for doing this with regex, you would need to use re.searchmethod, instead of re.matchmethod. The later one matches at the beginning of the string, so you would need to build the regex to match the entire string. You can rather do:

至于使用正则表达式执行此操作,您需要使用re.search方法,而不是re.match方法。后一个匹配字符串的开头,因此您需要构建正则表达式以匹配整个字符串。你可以这样做:

pm = re.compile(r"\bdog$")
has = pm.search("i love dog")

\bis word boundary. See Live Demo.

\b是词边界。见现场演示

To do the same with re.match, your regex should be - r".*dog$".

要对 做同样的事情re.match,您的正则表达式应该是 - r".*dog$"

pm = re.compile(r".*dog$")
has = pm.match("i love dog")

回答by Rohit Jain

Here's a slight modification of your code (that works):

这是对您的代码的轻微修改(有效):

import re
pm = re.compile(r'.*\b(dog)$')
has = pm.match("i love dog")
print(has)

The regex .*\b(dog)$maches anything (.*) then a word boundry (\b) and then your word (dog) and then the end of the line ($). Which is exactly what you want. Live demo here.

正则表达式.*\b(dog)$匹配任何内容 ( .*),然后是单词边界 ( \b),然后是您的单词 ( dog),然后是行尾 ( $)。这正是您想要的。现场演示在这里

回答by cad106uk

Get the word at the end of the string. Whatever that word is.

获取字符串末尾的单词。不管那个词是什么。

import re
pattern = re.compile(r"(\w+)$")
has = pm.search("i love dog")
print has.group(0)