7

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)
rajaditya_m
  • 289
  • 1
  • 4
  • 14

4 Answers4

20

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")
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    Thank you Mr. Jain. But this simple regex is my way of revising regular expressions for upcoming interviews and hence I am interested to know how to do it via python. – rajaditya_m Oct 18 '13 at 16:26
  • @rajaditya_m You should use `re.search` method instead of `re.match`. – Rohit Jain Oct 18 '13 at 16:27
  • Awesome. I just changed the match to search in my OLD regex and it also works. (In this case atleast). – rajaditya_m Oct 18 '13 at 16:33
  • `r".*dog"` would match stuff like 'asdfasdfdog', no? The word boundary might still be desired. – Hannele Oct 18 '13 at 16:36
  • I the question was about the regex and not the alter solutions, so i't is good to know what the regex is for this case – Ilya Libin Dec 29 '14 at 12:14
2

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.

1

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)
cad106uk
  • 471
  • 5
  • 5
1

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])