You are viewing a single comment's thread. Return to all 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)
Seems like cookies are disabled on this browser, please enable them to open this website
Incorrect Regex
You are viewing a single comment's thread. Return to all 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)