Group(), Groups() & Groupdict()
Create Date: March 31, 2019 at 03:35 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
group()
A group() expression returns one or more subgroups of the match.
Code
>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com')
>>> m.group(0) # The entire match
'username@hackerrank.com'
>>> m.group(1) # The first parenthesized subgroup.
'username'
>>> m.group(2) # The second parenthesized subgroup.
'hackerrank'
>>> m.group(3) # The third parenthesized subgroup.
'com'
>>> m.group(1,2,3) # Multiple arguments give us a tuple.
('username', 'hackerrank', 'com')
groups()
A groups() expression returns a tuple containing all the subgroups of the match.
Code
>>> import re
>>> m = re.match(r'(\w+)@(\w+)\.(\w+)','username@hackerrank.com')
>>> m.groups()
('username', 'hackerrank', 'com')
groupdict()
A groupdict() expression returns a dictionary containing all the named subgroups of the match, keyed by the subgroup name.
Code
>>> m = re.match(r'(?P<user>\w+)@(?P<website>\w+)\.(?P<extension>\w+)','myname@hackerrank.com')
>>> m.groupdict()
{'website': 'hackerrank', 'user': 'myname', 'extension': 'com'}
Task
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 .
Constraints
Output Format
Print the first occurrence of the repeating character. If there are no repeating characters, print -1
.
Sample Input
..12345678910111213141516171820212223
Sample Output
1
Explanation
..
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
.
method 1:
import re
#m = re.search(r"([a-zA-Z0-9])\1+", input().strip())
m = re.search(r"(\w(?!_))\1+", input().strip())
print(m.group(1) if m else -1)
method 2:
import re
m = re.findall(r"([a-zA-Z0-9])\1+", input().strip())
#m = re.findall(r"(\w(?!_))\1+", input().strip())
print(m[0] if m else -1)
What is the difference between re.match(), re.search() and re.findall() methods in Python?
re.match(), re.search() and re.findall() are methods of the Python module re.
The re.match() method
The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match. For example,
import re result = re.match(r'TP', 'TP Tutorials Point TP') print result.group(0)
Output:
TP
The re.search() method
The re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. For example
import re result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group(0)
Output:
Tutorials
The re.findall() method
The re.findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to use re.findall() always, it works like re.search() and re.match() both.
import re result = re.search(r'TP', 'TP Tutorials Point TP') print result.group()
Output
TP
Re.split()
Create Date: March 31, 2019 at 03:24 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
Check Tutorial tab to know how to to solve.
You are given a string consisting only of digits 0-9
, commas ,
, and dots .
Your task is to complete the regex_pattern
defined below, which will be used to re.split() all of the ,
and .
symbols in .
It’s guaranteed that every comma and every dot in is preceeded and followed by a digit.
Sample Input 0
100,000,000.000
Sample Output 0
100
000
000
000
regex_pattern = r"[,.]" # Do not delete 'r'.
import re
print("\n".join(re.split(regex_pattern, input())))
New Comment
Input()
Create Date: March 31, 2019 at 03:14 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
input()
In Python 2, the expression input() is equivalent to eval(raw _input(prompt)).
Code
>>> input()
1+2
3
>>> company = 'HackerRank'
>>> website = 'www.hackerrank.com'
>>> input()
'The company name: '+company+' and website: '+website
'The company name: HackerRank and website: www.hackerrank.com'
Task
You are given a polynomial of a single indeterminate (or variable), .
You are also given the values of and . Your task is to verify if .
Constraints
All coefficients of polynomial are integers.
and are also integers.
Input Format
The first line contains the space separated values of and .
The second line contains the polynomial .
Output Format
Print True
if . Otherwise, print False
.
Sample Input
1 4
x**3 + x**2 + x + 1
Sample Output
True
Explanation
Hence, the output is True
.
method 1:
x, k = map(int, input().split())
print(True if eval(input()) == k else False)
method 2:
x, k = map(int, input().split())
print(eval(input()) == k)