(Python)
Problem Statement:
You are given a string S.
Your task is to check whether S is a valid regex.
Input Format
The first line contains an integer T, the number of test cases.
The next T line 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
TrueFalse
Explanation
.*\+ : Valid regex.
.*+: Has the error multiple repeat. Hence, it is invalid.
Solution:
import re# validate regexdef is_valid_regex(T, patterns): for i in range(0, T): S = str(raw_input()) try: re.compile(S) print(True) except: print(False) if __name__ == "__main__": # User input T = int(input()) patterns = [r".*\+"] output = is_valid_regex(T, patterns)
Output:

Reference:
