Sort by

recency

|

326 Discussions

|

  • + 1 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

  • + 0 comments

    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")

  • + 0 comments

    true true this code is Als gave me

  • + 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)

  • + 0 comments

    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.

    import re
    res="False"
    pattern = r'(?:[*+?]|\{\d+(?:,\d*)?\})\+'    # ++,*+,?+,{..}+
    for _ in range(int(input())):
        inp=input()
        if re.findall(pattern,inp):
            res="False"
        else:
            try:
                re.compile(inp)
                res="True"
            except re.error as e:
                res="False"
        
        print(res)