Input Format
The first line contains integer , the number of test cases.
The next  lines contains the string .
Constraints
Output Format
Print "True" or "False" for each test case without quotes.

hackerrank incorrect regex problem solution in python programming

Problem solution in Python 3 programming language.

import re
for i in range(int(input())):
    try:
        s = input()
        re.compile(s)
    except:
        print("False")
        continue
    print("True")

Second solution

import re
for _ in range(int(input())):
    ans = True
    try:
        reg = re.compile(input())
    except re.error:
        ans = False
    print(ans)

Problem solution in Python 2 programming language.

import re
for i in range(int(raw_input())):
    try:
        s = raw_input()
        re.compile(s)
    except:
        print False
        continue
    print True