History
home
BDA 연혁
home

- 정규표현식 (regular expresson

정규 표현식이란?
정규 표현식의 이해
정규 표현식은 왜 사용하는가?
다양한 정규 표현식의 문법 이해
예제 코드를 이용한 정규표현식 이해하기
정규 표현식에 필요한 문법 패턴 이해
match, search 이해
정규표현식을 이용한 예제 코드
전화번호 패턴 찾기
이메일 주소 패턴 찾기
수업 예시 코드
# 예시 코드 import re re.search('[0-9]+','안녕하세요!dfasdfasdfasdfzsezsef!안녕?1004') re.search('[0-9]+-[0-9]+-[0-9]+','010-2323-23435') ## match와 search의 차이 sample_text = "Contact us at support@example.com or visit us on 2022/01/15." email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' date_pattern = r'\b(?:\d{1,4}[-/]\d{1,2}[-/]\d{1,4}|\d{1,2}[-/]\d{1,2}[-/]\d{1,4})\b' # re.match를 사용하여 텍스트의 시작에서 이메일 패턴 찾기 email_match = re.match(email_pattern, sample_text) if email_match: print("Matched email at the beginning:", email_match.group()) #match는?? # re.search를 사용하여 텍스트 전체에서 날짜 패턴 찾기 date_search = re.search(date_pattern, sample_text) if date_search: print("Found date anywhere in the text:", date_search.group()) ## 텍스트에서 이메일 주소, 전화번호, 날짜 추출하기 def extract_information(text): # 정규 표현식 패턴 정의 email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' phone_pattern = r'\b(?:\+\d{1,2}\s?)?(?:\(\d{1,4}\)|\d{1,4})[-.\s]?\d{1,12}\b' date_pattern = r'\b(?:\d{1,4}[-/]\d{1,2}[-/]\d{1,4}|\d{1,2}[-/]\d{1,2}[-/]\d{1,4})\b' # 이메일 주소 추출 emails = re.findall(email_pattern, text) print("Email Addresses:", emails) # 전화번호 추출 phone_numbers = re.findall(phone_pattern, text) print("Phone Numbers:", phone_numbers) # 날짜 추출 dates = re.findall(date_pattern, text) print("Dates:", dates) sample_text = """ Contact us at support@example.com or call +1 (555) 123-4567. You can also reach us on 2022/01/15. Our office hours are 9:00 AM to 5:00 PM. """ extract_information(sample_text)
Python
복사