Python @ DjangoSpin

PyPro #90 Match two of a kind characters in a string

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Match two of a kind characters in a string in Python

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.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply