1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import re #반복을 나타내는 기호 # ? 앞문자가 문자가 0 또는 1 번 ( 0 or 1 ) # * 앞문자가 0 번 또는 그이상 ( 0 or 1 more) # + 앞문자가 1 번 또는 그이상 ( 1 more) #패턴 정의 pattern1 = re.compile( 'D?ABC' )# ABC or DABC pattern2 = re.compile( 'D*ABC' )# ABC or DDABC or DDDABC.... pattern3 = re.compile( 'D+ABC' )# DABC or DDABC or DDDABC... string = 'DDDDDDDd ABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) print(pattern3.search(string)) string = 'DDDDDDDd DABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) print(pattern3.search(string)) string = 'DDDDDDDd DDABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) print(pattern3.search(string)) string = 'DDDDDDDd DDDABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) print(pattern3.search(string)) #결과 ======ex) DDDDDDDd ABC re.Match object; span=( 9 , 12 ), match= 'ABC' re.Match object; span=( 9 , 12 ), match= 'ABC' None ======ex) DDDDDDDd DABC re.Match object; span=( 9 , 13 ), match= 'DABC' re.Match object; span=( 9 , 13 ), match= 'DABC' re.Match object; span=( 9 , 13 ), match= 'DABC' ======ex) DDDDDDDd DDAABC re.Match object; span=( 12 , 15 ), match= 'ABC' re.Match object; span=( 12 , 15 ), match= 'ABC' None ======ex) DDDDDDDd DDDABC re.Match object; span=( 11 , 15 ), match= 'DABC' re.Match object; span=( 9 , 15 ), match= 'DDDABC' re.Match object; span=( 9 , 15 ), match= 'DDDABC' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import re #반복을 나타내는 기호 # {n} 앞문자가 n번 반복 # {m,n} 앞문자가 m번 이상 n번 이하 반복 #패턴 정의 pattern1 = re.compile( 'D{1}ABC' ) pattern2 = re.compile( 'D{2,5}ABC' ) string = 'DDDDDDDd ABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) string = 'DDDDDDDd DABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) string = 'DDDDDDDd DDABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) string = 'DDDDDDDd DDDABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) string = 'DDDDDDDd DDDDDDABC' print( '======ex) ' ,string) print(pattern1.search(string)) print(pattern2.search(string)) #결과 ======ex) DDDDDDDd ABC None None ======ex) DDDDDDDd DABC re.Match object; span=( 9 , 13 ), match= 'DABC' None ======ex) DDDDDDDd DDABC re.Match object; span=( 10 , 14 ), match= 'DABC' re.Match object; span=( 9 , 14 ), match= 'DDABC' ======ex) DDDDDDDd DDDABC re.Match object; span=( 11 , 15 ), match= 'DABC' re.Match object; span=( 9 , 15 ), match= 'DDDABC' ======ex) DDDDDDDd DDDDDDABC re.Match object; span=( 14 , 18 ), match= 'DABC' re.Match object; span=( 10 , 18 ), match= 'DDDDDABC' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import re #패턴 정의 pattern1 = re.compile( '[가-힣]' )#한글 패턴 찾기 pattern2 = re.compile( '[^가-힣]' )#한글 패턴이 아닌것 찾기 string = "안녕hello" print(pattern1.search(string)) print(pattern2.search(string)) #결과 re.Match object; span=( 0 , 1 ), match= '안' re.Match object; span=( 2 , 3 ), match= 'h' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import re #첫번째 단어를 리턴 pattern1 = re.compile( '[a-zA-Z]+' ) print( 'ex1)' ) string = "hello world" print(pattern1.search(string))#문자열 전체를 검색하여 정규식에 해당하는 패턴을 리턴 print(pattern1.match(string))#문자열 처음부터 검색하여 정규식에 해당하는 패턴을 리턴 print( 'ex2)' ) string = "11hello world" print(pattern1.search(string))#문자열 전체를 검색하여 정규식에 해당하는 패턴을 리턴 print(pattern1.match(string))#문자열 처음부터 검색하여 정규식에 해당하는 패턴을 리턴 #결과 ex1) re.Match object; span=( 0 , 5 ), match= 'hello' re.Match object; span=( 0 , 5 ), match= 'hello' ex2) re.Match object; span=( 2 , 7 ), match= 'hello' None |
'python' 카테고리의 다른 글
python 가상환경 설정 (0) | 2020.01.02 |
---|---|
크롤링 연습 11. google sheet에 데이터 저장 (0) | 2019.12.27 |
정규표현식 연습 1. (0) | 2019.12.26 |
자주 쓰는 string 함수 (0) | 2019.12.26 |
크롤링 연습 10. session을 이용한 post 예제 (0) | 2019.12.26 |