find whether the string starts and ends with the same word

Posted by Ajax on Stack Overflow See other posts from Stack Overflow or by Ajax
Published on 2013-06-30T09:49:54Z Indexed on 2013/06/30 10:21 UTC
Read the original article Hit count: 234

Filed under:
|

I am trying to check whether the string starts and ends with the same word. egearth.

s=raw_input();
m=re.search(r"^(earth).*(earth)$",s)
if m is not None:
    print "found"

my problem is when the string consists only of one word eg: earth

At present I have hard coded this case by

if m is not None or s=='earth':
    print "found"

Is there any other way to do this?

EDIT:

words in a string are separated by spaces. looking for a regex solution

some examples:

"earth is earth" ,"earth", --> valid

"earthearth", "eartheeearth", "earth earth mars" --> invalid

© Stack Overflow or respective owner

Related posts about python

Related posts about regex