Using a variant over the regex provided by the answer of karthik manchala, and noticing that you want the same output as given in your question here is a complete code example:
import re
inputText = """The dodo was one of the sturdiest birds.
An educated termite may learn how to operate a phonograph,
but it's unlikely. I sense that an amalgam that includes
magma will enlighten Papa."""
regex = re.compile(r"((\w{2})\w*\2)")
answer = regex.findall(inputText)
print("answer = {}".format(answer))
Note that in addition to capturing the group of the two first characters, (\w{2})
, allowing for arbitrary number of characters inbetween, \w*
, and finally matching the first group at end, \2
, I’ve surrounded the entire regexp with another group of parentheses, ( ... )
.
When running this the entire word will be \1
, whilst the two character group is \2
, and using findall
will find all occurences and return a list of tuples, where each tuple is the capture groups.
solved Python matching word by using regex