How can I use regex to search for repeating word in a string in Python? -


is possible search repeating word in string use regex in python?

for instance:

string = ("hello world hello mister rain")  re.search(r'[\w ]+[\w ]+[\w ]+[\w ]+[\w ]', string) 

can won't have repeat [\w ]+[\w ]. can't specify [\w ]*5 instead?

i think easier using plain python:

from collections import counter  string = "hello world hello mister rain" # note: no () needed words = string.split()  word, count in counter(map(str.lower, words)).iteritems():     if count > 1:         print "the word '{}' repeated {} times.".format(word, count) 

Comments