백레퍼런스

Python 정규 표현식

Maria Eugenia Inzaugarat

Data Scientist

번호가 매겨진 그룹

 

 

Python 정규 표현식

번호가 매겨진 그룹

 

Python 정규 표현식

번호가 매겨진 그룹

 

text = "Python 3.0 was released on 12-03-2008."
information = re.search(r'(\d{1,2})-(\d{2})-(\d{4})', text)

information.group(3)
'2008'
information.group(0)
'12-03-2008'
Python 정규 표현식

이름이 지정된 그룹

  • 그룹에 이름 지정하기

 

Python 정규 표현식

이름이 지정된 그룹

  • 그룹에 이름 지정하기
text = "Austin, 78701"

cities = re.search(r"(?P<city>[A-Za-z]+).*?(?P<zipcode>\d{5})", text)
cities.group("city")
'Austin'
cities.group("zipcode")
'78701'
Python 정규 표현식

백레퍼런스

  • 캡처 그룹을 사용해 그룹을 다시 참조하기

 

Python 정규 표현식

백레퍼런스

  • 번호가 매겨진 캡처 그룹을 사용해 다시 참조하기

 

sentence = "I wish you a happy happy birthday!"

re.findall(r"(\w+)\s ", sentence)
Python 정규 표현식

백레퍼런스

  • 번호가 매겨진 캡처 그룹을 사용해 다시 참조하기

 

sentence = "I wish you a happy happy birthday!"

re.findall(r"(\w+)\s\1", sentence)
['happy']
Python 정규 표현식

백레퍼런스

  • 번호가 매겨진 캡처 그룹을 사용해 다시 참조하기

 

sentence = "I wish you a happy happy birthday!"

re.sub(r"(\w+)\s\1", r"\1", sentence)
'I wish you a happy birthday!'
Python 정규 표현식

백레퍼런스

  • 이름이 지정된 캡처 그룹을 사용해 다시 참조하기

 

sentence = "Your new code number is 23434. Please, enter 23434 to open the door."

re.findall(r"(?P<code>\d{5}).*?(?P=code)", sentence)
['23434']
Python 정규 표현식

백레퍼런스

  • 이름이 지정된 캡처 그룹을 사용해 다시 참조하기

 

sentence = "This app is not working! It's repeating the last word word."
re.sub(r"(?P<word>\w+)\s(?P=word)", r"\g<word>", sentence)
'This app is not working! It's repeating the last word.'
Python 정규 표현식

연습해 봅시다!

Python 정규 표현식

Preparing Video For Download...