Sort by

recency

|

322 Discussions

|

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

    I solve this problem with Python 2 and raw_string. But in Python 3, I don't know how it's possible cause we always have the SyntaxError with '\'.

    Nevertheless, here my code :

    import re
    
    T = int(input())
    
    for T_itr in range(T):
        regex = input()
    
        try:
            re.compile(regex)
            print("True")
        except re.error:
            print("False")
            
    
  • + 0 comments

    Try Like this

    import re

    T = int(input()) invalid_repeats = ['++', '**', '??', '+', '+', '?', '?', '+?', '??']

    for _ in range(T): S = input().strip()

    # Basic invalid pattern check
    is_invalid = any(rep in S for rep in invalid_repeats)
    
    if is_invalid:
        print("False")
        continue
    
    try:
        re.compile(S)
        print("True")
    except re.error:
        print("False")
    
  • + 1 comment

    how to solve this code

  • + 0 comments

    Accepted solution for python 2

    import re
    for _ in range(int(raw_input())):
        try:
            re.compile(raw_input().strip())
            print(True)
        except re.error:
            print(False)