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)
5
  • Are your words always space separated? Commented Oct 18, 2013 at 16:21
  • Post your python code. Commented Oct 18, 2013 at 16:21
  • Yes. NO hypenated word required> Also FYI I tried ( |^)dog$ with same result !!! Commented Oct 18, 2013 at 16:22
  • 1
    possible duplicate of What is the difference between Python's re.search and re.match? Commented Oct 18, 2013 at 16:26
  • df.str.replace(r"(\W|^)(dog|cat)$", ' ') this worked for a list of terms in python pandas Commented Apr 7, 2022 at 2:24

4 Answers 4

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")
Sign up to request clarification or add additional context in comments.

5 Comments

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 You should use re.search method instead of re.match.
Awesome. I just changed the match to search in my OLD regex and it also works. (In this case atleast).
r".*dog" would match stuff like 'asdfasdfdog', no? The word boundary might still be desired.
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
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.

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.