Python @ DjangoSpin

PyPro #74 Match three-of-a-kind characters from 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 three-of-a-kind characters from a string in Python

Match three-of-a-kind characters from a string in Python

Write a Regular Expression in Python to match three-of-a-kind characters from a string containing characters other than a newline e.g. 'abc123abc123abc'.

Match three-of-a-kind characters from a string

import re

sourceString = 'abc123abc123abc'

matchObject = re.search(r'.*?(.).*?\1.*?\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