简体   繁体   English

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

[英]Regex to find last word in a string (Python)

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$" .我写了这样的东西"(\W|^)dog$" (Check if last word in the sentence is 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 正则表达式编辑器中对此进行了测试,它似乎有效。

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)

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. 由于您只需要最后一个单词, str.rsplit可用于从end开始拆分,并将1作为第二个参数传递,只执行一次拆分。 Then get the last element of the returned list. 然后获取返回列表的最后一个元素。


As for doing this with regex, you would need to use re.search method, instead of re.match method. 至于使用正则表达式执行此操作,您需要使用re.search方法,而不是re.match方法。 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: 你可以宁愿做:

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

\\b is word boundary. \\b是单词边界。 See Live Demo . 观看现场演示

To do the same with re.match , your regex should be - r".*dog$" . 为了对re.match做同样的re.match ,你的正则表达式应该是 - r".*dog$"

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

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 ( $ ). 正则表达式.*\\b(dog)$匹配任何东西( .* )然后是单词边界( \\b )然后是你的单词( dog )然后是行尾( $ )。 Which is exactly what you want. 这正是你想要的。 Live demo here . 现场演示这里

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)

You can do你可以做

import re
text = "Python was conceived in the late 1980"
s = re.findall(r"\s(\w+)$", text)
print(s)

Or要么

s = re.split("\s", text)
print(s[-1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM