Algorithm


Problem Name: Python - Incorrect Regex

Problem Link: https://www.hackerrank.com/challenges/incorrect-regex/problem?isFullScreen=true

In this HackerRank Functions in PYTHON problem solution,

You are given a string S .

Your task is to find out whether S is a valid regex or not.

Input Format

The first line contains integer T, the number of test cases.
The next T lines contains the string S.

Constraints

0 < T <100

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.

 

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


import re

count = int(input())
for i in range(count):
    try:
        regex = re.compile(input())
        print(True)
    except:
        print(False)
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] itertools.combinations() in PYTHON solution in Hackerrank
Next
[Solved] Set .add() in PYTHON solution in Hackerrank