Incorrect Regex

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 Implementation


import re
T = int(input())
for i in range(0, T):
    regex_raw = input()
    try:
        regex = re.compile(regex_raw)
    except re.error:
        print(False)
    else:
        print(True)
Copied!

Leave a Reply

Your email address will not be published. Required fields are marked *