Wednesday, July 22, 2020

Incorrect Regex-HackerRank Solution

 Incorrect Regex-HackerRank Solution

 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 Solution-
import re
for _ in range(int(input())): #Ask for int input    
    try:
        re.compile(input()) #use compile method which returns True if input is an regular expression
        print ("True")
    except re.error as e: #If it's an error print False
        print ("False")
To read more about re library and methods such as compile follow this link-https://docs.python.org/3/library/re.html

0 comments:

Post a Comment

Feel free to share your thoughts and suggestion...