Re.start() & Re.end() HackerRank Solution In Python

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 _indexend _index).
If no match is found, print (-1, -1).

Sample Input

aaadaaaa

Sample Output

(0, 1)  (1, 2)(4, 5)

Solution – Re.start() & Re.end() In Python | HackerRank

import res = input()n = input()r = re.compile(n)m = r.search(s)if not m:    print("(-1, -1)")while m:    print("({}, {})".format(m.start(), m.end()-1))    m = r.search(s, m.start() + 1)

NOTE: The problem solved above, Re.start() & Re.end(), was generated by HackerRank and the solution was brought by the admin of  CodingSolutions for educational purpose. Got any issues with the code? Ask your questions in the comment box and I shall attend to it.

Comments