Prepare > Python > Errors and Exceptions > Incorrect Regex

2023. 8. 24. 10:04HackerRank-Python

 

Incorrect Regex | HackerRank

Check whether the regex is valid or not.

www.hackerrank.com

 

문제


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.

Output Format

Print "True" or "False" for each test case without quotes.

 

=> 문자열S를 T개 줄건데 각각이 유효한 정규표현식인지 True, False로 판별하라

 

 

 

코드


import re
T = int(input())
for _ in range(T):
    try:
        re.compile(input())
        print(True)
    except Exception:
        print(False)
  • try except 구문: re.complie()이 컴파일에 성공하면 True를 출력, 실패하면 False 출력

 

 

 

노트


import re

  • 정규식을 처리하는 모듈

 

re.compile()

re.complie(pattern, [flags])
  • pattern 분석
  • 분석된 pattern을 기반으로 정규표현식 객체 생성 및 리턴

 

 

 

 

 

참조


 

HackerRank Incorrect Regex solution in python

HackerRank Incorrect Regex solution in python2, python3, and pypy3, pypy programming language with practical program code example and explaination

programs.programmingoneonone.com

 

https://bard.google.com/share/efad3a98317f

 

bard.google.com