Incorrect Regex in Python - HackerRank Solution

Incorrect Regex in Python - HackerRank Solution
Incorrect Regex in Python - HackerRank Solution


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 integer T, the number of test cases.
The next T lines contains the string S.

Constraints :

  • 0 < T < 100

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 repeat. Hence, it is invalid.



Solution :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Incorrect Regex in Python - Hacker Rank Solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
# Python 3
# Incorrect Regex in Python - Hacker Rank Solution START
import re;

N = int(input())
for _ in range(N):
    try:
        re.compile(input())
        Output = True
    except re.error:
        Output = False
    
    print(Output)
# Incorrect Regex in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
No Comment
Add Comment
comment url