Python Group(), Groups() & Groupdict()
1 min read

Python Group(), Groups() & Groupdict()

Python Group(), Groups() & Groupdict()


A group() expression returns one or more subgroups of the match.

>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>>       # The entire match 
'[email protected]'
>>>       # The first parenthesized subgroup.
>>>       # The second parenthesized subgroup.
>>>       # The third parenthesized subgroup.
>>>,2,3)   # Multiple arguments give us a tuple.
('username', 'hackerrank', 'com')


A groups() expression returns a tuple containing all the subgroups of the match.

>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','[email protected]')
>>> m.groups()
('username', 'hackerrank', 'com')


A groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.

>>> m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','[email protected]')
>>> m.groupdict()
{'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}


You are given a string .
Your task is to find the first occurrence of an alphanumeric character in  (read from left to right) that has consecutive repetitions.

Input Format

A single line of input containing the string .


Output Format

Print the first occurrence of the repeating character. If there are no repeating characters, print -1.

Sample Input


Sample Output



.. is the first repeating character, but it is not alphanumeric.
1 is the first (from left to right) alphanumeric repeating character of the string in the substring 111.

Solution in Python

import re
match ="([a-zA-Z1-9])\1",input())
print( if match else -1)

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add to your ad blocking whitelist or disable your adblocking software.