Find if a line starts and ends with the same word

I am trying to check if a line starts and ends with the same word. e.g. earth .

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

My problem is that the string consists of only one word , for example: earth

I currently hardcoded this case

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

Is there any other way to do this?

EDIT:

The words

in a line are separated by spaces. finding a regex solution

some examples :

"earth - earth", "earth", β†’ valid

"globe", "earth stone", "earth earth" β†’ invalid

+4
source share
5 answers

You can use backlink in regex

 ^(\w+\b)(.*\b\1$|$) 

This will match the string only if it

  • begins and ends with the same word
  • has one word
+4
source

Use str.startswith and str.endswith instead.

 >>> 'earth'.startswith('earth') True >>> 'earth'.endswith('earth') True 

You can simply combine them into one function:

 def startsandendswith(main_str): return main_str.startswith(check_str) and main_str.endswith(check_str) 

And now we can call it:

 >>> startsandendswith('earth', 'earth') True 

If, however, if the code matches the words and not part of the word, it might be easier to split the line and then check if the first and last word is the line you want to check:

 def startsandendswith(main_str, check_str): if not main_str: # guard against empty strings return False words = main_str.split(' ') # use main_str.split() to split on any whitespace return words[0] == words[-1] == check_str 

Launch:

 >>> startsandendswith('earth', 'earth') True >>> startsandendswith('earth is earth', 'earth') True >>> startsandendswith('earthis earth', 'earth') False 
+7
source

You can use str.startswith and str.endswith :

 >>> strs = "earthfooearth" >>> strs.startswith('earth') and strs.endswith("earth") True >>> strs = "earth" >>> strs.startswith('earth') and strs.endswith("earth") True 

Update:

If words are separated by spaces and the start and end lines are unknown, use str.split and str.rsplit :

 >>> strs = "foo bar foo" >>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1] True # single word >>> strs = "foo" >>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1] True >>> strs = "foo bar ffoo" >>> strs.split(None, 1)[0] == strs.rsplit(None, 1)[-1] False 
+3
source

Here:

 X = words.split() X[:1] == X[-1:] 

Slicing makes it work for empty lines as well, and applies well to any number of words. If words cannot be empty, use

 X[0] == X[-1] 
+3
source

Well, if you absolutely want a regular expression, you can use lookarounds, since they don't consume characters.

 >>>import re >>>s1 = 'earth is earth' >>>s2 = 'earth' >>>m = re.search(r"^(?=(earth)).*(earth)$",s1) >>>m.group(1) 'earth' >>>m.group(2) 'earth' >>>m = re.search(r"^(?=(earth)).*(earth)$",s2) >>>m.group(1) 'earth' >>>m.group(2) 'earth' 

For any line, you can use this:

 ^(?=([A-Za-z]+)).*(\1)$ 

I assume that the words are only symbols of the alphabet. If you mean words as characters without spaces, you can go instead of \S instead of [A-Za-z] .

EDIT: Okay, it looks like he is bigger. I think this may come up:

 ^(?=(earth\b)).*((?:^|\s)\1)$ 

For working land. For any word stored in a variable named word ;

 >>> word = 'earth' # Makes it so you can change it anytime >>> pattern = re.compile('^(?=(' + word + '\b)).*((?:^|\s)\1)$') >>> m.search(pattern, s) 

Accepts:

 earth is earth earth 

Deviation:

 earthearth eartheearth earthis earth 

And after that, remove the captured groups or check if the group is empty or not.

Added bit: (?:^|\s) , which checks if the word you are looking for is the only one in the "sentence" or the word in the sentence.

+1
source

Source: https://habr.com/ru/post/1488936/


All Articles