Reading Time: 1 minutes
Match two of a kind characters in a string in Python
Write a Regular Expression in Python to match two of a kind characters in a string containing characters other than a newline e.g. 'abc123abc'.
import re sourceString = 'abc123abc' matchObject = re.search(r'.*?(.).*?\1', sourceString) print( matchObject.groups() ) # ('a',)
For clarity, try it out on Regex101. To learn more about Regular Expressions in Python, click here.