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

时间:2022-09-12 19:28:17

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 tested this in the Rubular regex editor and it seems to work.

我在Rubular正则表达式编辑器中对此进行了测试,它似乎有效。

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)

3 个解决方案

#1


17  

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.rsplit can be used to start splitting from end, and passing 1 as 2nd argument, will only perform split only once. Then get the last element of the returned list.

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


As for doing this with regex, you would need to use re.search method, instead of re.match method. 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")

\b is 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")

#2


1  

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)然后是行尾($)。这正是你想要的。现场演示。

#3


0  

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)

#1


17  

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.rsplit can be used to start splitting from end, and passing 1 as 2nd argument, will only perform split only once. Then get the last element of the returned list.

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


As for doing this with regex, you would need to use re.search method, instead of re.match method. 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")

\b is 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")

#2


1  

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)然后是行尾($)。这正是你想要的。现场演示。

#3


0  

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)