Regex to find last word in a string (Python)

Question:

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)

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

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

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)
Asked By: rajaditya_m

||

Answers:

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.


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:

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

b is word boundary. See Live Demo.

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

pm = re.compile(r".*dog$")
has = pm.match("i love dog")
Answered 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.

Answered By: user1120144

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)
Answered By: cad106uk

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])
Answered By: Baboucarr Badjie
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.