Incorrect Regex HackerRank solution in Python

In this article, we will solve the HackerRank problem using python programming.

Problem

You are given a string S.

Your task is to find out whether S is a valid regex or not.

Input Format

The first line contains an integer T, the number of test cases.

The next T lines contain the string S.

Output Format

Print "True" or "False" for each test case without quotes.

Sample Input

2

.*\+

.*+

Sample Output

True

False

Explanation

.*\+ : Valid regex.

.*+: Has the error multiple repeats. Hence, it is invalid.

Solution in Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Incorrect Regex HackerRank solution in Python

# import regex
import re

# take a input of test cases
T = int(input())


for test in range(T):
    # check the regex is valid or not 
    try:
        re.compile(input())
        print(True)
    except Exception:
        print(False)

Next Post Previous Post
No Comment
Add Comment
comment url