Hello Programmers, In this post, you will know how to solve the Re.start() & Re.end() in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Re.start() & Re.end() in Python HackerRank Solutions
problem
start() & end()
These expressions return the indices of the start and end of the substring matched by the group.
Code :
>>> import re >>> m = re.search(r'\d+','1234') >>> m.end() 4 >>> m.start() 0
Task :
You are given a string S.
Your task is to find the indices of the start and end of string k in S.
Input Format :
The first line contains the string S.
The second line contains the string k.
Constraints :
- 0 < len(s) < 100
- 0 < len(k) < len(s)
Output Format :
Print the tuple in this format: (start _index, end _index).
If no match is found, print (-1, -1).
Sample Input :
aaadaa aa
Sample Output :
(0, 1) (1, 2) (4, 5)
Re.start() & Re.end() in Python HackerRank Solutions
import re S, k = input(), input() matches = re.finditer(r'(?=(' + k + '))', S) anymatch = False for match in matches: anymatch = True print((match.start(1), match.end(1) - 1)) if anymatch == False: print((-1, -1))
Disclaimer: The above Problem (Re.start() & Re.end() in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.