We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Incorrect Regex
Incorrect Regex
Sort by
recency
|
326 Discussions
|
Please Login in order to post a comment
my solution: import re for i in range(int(input())): try: re.compile(input()) print(True) except re.error: print(False) but it is giving output as TRUE TRUE and hence failing the sample test case. Although i did solve it using python 2 can anyone help me with a pypy3 solution
This works for Python 3 import re if name == 'main': n = int(raw_input()) for i in range(n): try: re.compile(raw_input()) print("True") except re.error: print("False")
true true this code is Als gave me
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)
python 3 solution
python added support for possesive quantifiers (*+,++,?+,{ }+) from python 3.11, but the expected outputs are not updated to align with the updated python version. so we need to handle possesive quantifiers separately.