Incorrect Regex in Python HackerRank Solution

Hello coders, In this post, you will learn how to solve Incorrect Regex in Python HackerRank Solution. This problem is a part of the Python Hacker Rank series.

Incorrect Regex in Python HackerRank Solution
Incorrect Regex in Python HackerRank Solution

Incorrect Regex in Python HackerRank Solution

problem

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.

Incorrect Regex in Python HackerRank Solution

import re;
N = int(input())
for _ in range(N):
    try:
        re.compile(input())
        Output = True
    except re.error:
        Output = False
    print(Output)

Disclaimer: The above Problem (Incorrect Regex in Python) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Sharing Is Caring

Leave a Comment