데이터&AI
정규표현식(regex, 레젝스) with 파이썬!! (3) - 예제
일등박사
2023. 1. 9. 08:48
이제!! 실제 정규표현식을 활용한 예제들을 알아보자!!
전화번호
전화번호는 여러방식으로 표현한다!!
(010) 0000 0000
(010)0000-0000
010-0000-0000
010 0000 0000
010.0000.0000
이 전화번호들은 아래와 같이 찾을 수 있다
target_sentence = """
(010) 0000 0000
(010)0000-0000
010-0000-0000
010 0000 0000
010.0000.0000
"""
my_regex = "\(?010[ -\.]\d+[ -\.]\d+"
re.findall(my_regex,target_sentence)
우편번호
우리나라의 우편번호는 간단하다
5개의 숫자로 구성된다
00000
target_sentence = """
00000
"""
my_regex = "\d+"
re.findall(my_regex,target_sentence)
우편번호
우리나라의 우편번호는 간단하다
5개의 숫자로 구성된다
00000
target_sentence = """
00000
"""
my_regex = "\d{5}"
re.findall(my_regex,target_sentence)
IP주소
IP 주소는 숫자 네개를 마침표로 구분한다!!
그리고 각 자리수는 255가 최대값이다!!
127.0.0.1
0.0.0.0
255.255.255.255
my_regex = "(((\d{1,2})|(1\d{2})| (2[0-4]\d) | (25[0-5]))\.){3}((\d{1,2})|(1\d{2}) |(2[0-4]\d) | (25[0-5]))"
website 주소
https , ip 주소 포트번호등등이 조합된 다양한 주소들이있다
http://localhost:8888
http:/localhost/index.php?aab=1&c=2
find_myName = re.compile("https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?")
이메일 주소
find_myName = re.compile("(\w+\.)*\w+@(\w+\.)+[A-Za-z]+")
HTML 주석
target_sentence = """
<!-- html comments -->
"""
find_myName = re.compile("<!-{2,}.*?-{2,}>")
re.findall(find_myName,target_sentence)
PYTHON, JAVASCRIPT 주석
target_sentence = """
// python comments
a=3
"""
find_myName = re.compile("//.*")
re.findall(find_myName,target_sentence)
주민번호
target_sentence = """
790814-1444444
"""
find_myName = re.compile("\d{6}-\d{7}")
re.findall(find_myName,target_sentence)
위 내용은 "손에 잡히는 정규표현식" 을 참고로 작성하였습니다!^^
https://www.aladin.co.kr/shop/wproduct.aspx?ItemId=4342103
손에 잡히는 정규표현식
손에 잡히는 정규표현식
www.aladin.co.kr