• + 0 comments

    Explanation: sys.stdin.readline() reads raw input lines safely.

    .strip() removes the newline character at the end.

    We compile the pattern using re.compile().

    If it's a valid regex, True is printed; otherwise, False.

    import re import sys

    n = int(sys.stdin.readline()) for _ in range(n): pattern = sys.stdin.readline().strip() try: re.compile(pattern) print(True) except re.error: print(False)