Categories
Coding Computer Science Python

Solving Problem: Incorrect Regex

(Python)

You are given a string S.
Your task is to check whether S is a valid regex.

Input Format

The first line contains an integer T, the number of test cases.
The next T line 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.

validate_regex.py
Python
import re
# validate regex
def is_valid_regex(T, patterns):
for i in range(0, T):
S = str(raw_input())
try:
re.compile(S)
print(True)
except:
print(False)
if __name__ == "__main__":
# User input
T = int(input())
patterns = [r".*\+"]
output = is_valid_regex(T, patterns)

Incorrect Regex | HackerRank

Priyanka B.'s avatar

By Priyanka B.

Hello and welcome to my little corner of internet!! I am a techie. I am very interested to discover and innovate new advances in science and technology. Blogging is one of my hobbies which I think is very useful for broadening my knowledge horizons and help me grow my skills. Apart from blogging I have also little taste in artistic skills and literature, which can keep my writing and posts tangy.

Whether you stumbled in by chance or came here on purpose, I hope you find something that sparks your curiosity or makes you think a little deeper. Thanks for stopping by!!

Leave a comment