본문 바로가기

정규식(파이썬)

Ex09_reg4_compile.py

'''
    # compile
     - 동일한 정규표현식을 매번 다시 쓰기 번거로움을 해결
     - compile로 해당표현식을 re.RegexObject 객체로 저장하여 사용가능
'''

import re
#문자와 하이픈 섞인거 골뱅이 문자 점
pattern = re.compile(r'[\w-]+@[\w.]+')
result = pattern.search('test@gmail.com hahaha good') #써치는 첫번쨰꺼하나 파인드올은 모두
if result:
    print(result.group())
#----------------------------------------
webs = ['http://www.test.co.kr',
        'https://www.test1.com',
        'http://www.test.com',
        'ftp://www.test.com', #놉
        'http:://www.test.com', #놉
       'htp://www.test.com', #놉
       'http://www.google.com',
       'https://www.homepage.com.']

# ? 0개이거나 1개이거나 문자숫자점 문자로 끝내고자합니다.($)
pattern = re.compile(r'https?://[\w.]+\w+$')
result = list(map(lambda w:pattern.search(w), webs)) #인자가 들어왔을시 인자를 : 뒤로 처리 / map으로 요소를 하나하나 꺼내 계속
#print(result)

for url in result:
    if url:
        print(url.group())