Sunday, July 28, 2019

Incorrect Regex - Hacker Rank Solution

You are given a string .
Your task is to find out whether  is a valid regex or not.
Input Format
The first line contains integer , the number of test cases.
The next  lines contains the string .
Constraints
Output Format
Print "True" or "False" for each test case without quotes.
Sample Input
2
.*\+
.*+
Sample Output
True
False
Explanation
.*\+ : Valid regex.
.*+: Has the error multiple repeat. Hence, it is invalid.

Incorrect Regex - Hacker Rank Solution
import re
for i in range(int(raw_input())):
    try:
        s = raw_input()
        re.compile(s)
    except:
        print False
        continue
    print True    

3 comments:

  1. My code. It works well for some test cases, needs modification though.
    import re
    n = int(input())
    for i in range(n):
    text = input()
    x = ".*\+"
    #re.compile(text)
    if x == text:
    print(True)
    continue
    else:
    print(False)
    i = i+1

    ReplyDelete
  2. import re
    for i in range(int(input())):
    try:
    s=input()
    re.compile(s)
    print(True)
    except:
    print(False)

    ReplyDelete

Powered by Blogger.