[210728 TIL] Python

Choi Rim·2021년 7월 28일
0

Python

목록 보기
18/20
post-thumbnail

정규식

\

  • 역슬래시
  • 이스케이프 시퀀스의 시작을 알려줌
  • 정규식 기호와 인식해야 할 문자를 구별해줄 수 있음

re.sub

  • re.sub('패턴', '바꿀문자열', '문자열', 바꿀횟수)
import re
def is_valid(string):
    while True:
      if "()" in string:
        string = re.sub("[(][)]","",string)
      elif "[]" in string:
        string = re.sub("[\[][\]]","",string)
      elif "{}" in string:
        string = re.sub("[{][}]","",string)
      else:
        break
    return False if string else True
  • 만약 여는 기호와 닫는 기호가 함께 붙어있으면 빈 문자로 바꿔줌
  • 없다면 while문을 빠져나옴

return if else

if string:
	return False
else:
	return True
return False if string else True
  • 파이썬에서는 if else 문과 return의 결합문을 한 줄로 나타낼 수 있다.
profile
https://rimi0108.github.io/

0개의 댓글