2023. 11. 30. 21:45ㆍ파이썬/문법
expression = ^b...s$ : 'b'로 시작하고 's'로 끝나는 다섯 글자 문자열과 일치 함
match or no match?
<string>
basvs : match
babcds : no match (글자 수 오류)
bsssa : no match (끝나는 문자 오류 -> ^b...a$의 표현은 match 가능
Blaws : no match (대소문자 구분 오류)
bn ans : no match (공백도 카운팅)
[정규식 syntax]
메타 문자 : RegEx 엔진에 의해 특별한 방식으로 해석되는 문자
• . ^ $ * + ? { } [ ] \ | ( )
대괄호 : []
일치시키려는 문자 집합을 지정
expression : [qwer]
<string>
match or no match?
q : 1match
qw : 2match
qwe : 3match
hey jude : 2 match
good luck! : no match
대괄호안에 "-"를 사용하여 문자 범위 지정 가능
ex)
[a-e] = [abced]
[1-4] = [1234]
[0-39]=[01239]
[a-z]=모든 소문자 알파벳 문자
^[]는 문자 집합 반전
ex)
[^abc] : 'a', 'b', 'c'를 제외한 모든 문자를 의미
['^0-9] : 숫자가 아닌 문자를 의미
-마침표 "." : 모든 단일 문자와 일치 (newlone \n 제외)
experession : ...
<string>
ab : no match
abc : 1match
wqwreq : 2match
qwertqweqa : 3match
-교대 "|" = or
expression : a|b|c
<string >
cde : 1match
acasbva : 5match
-캐럿 : "^" : 문자열이 특정 문자로 시작하는지 학인하는 데 사용
expression : ^a
<string>
a : 1match
abd : 1match
qw : no match
expression : ^qr
^qr : 1match
aqer : no match
달러 "$" : 문자열이 특정 문자로 끝나는지 확인하는 데 사용
expression : a$
<string>
a : 1 match
anaconda : 1match
python : no match
별 "*" : 남겨진 패턴의 0개 이상의 발생과 일치
expression : ma*n
<string>
mn : 1match
man : 1match
maaan : 1match
main : no match #m이나 a 뒤에 n이 와야함
woman : 1match
더하기 "+" : 하나 이상의 항목과 일치
expression : ma+n
mn : no match
man : 1match
maaan : 1match
main : no match (a뒤에 n이 안옴)
woman : 1match
물음표 "?" : 0개 또는 1개의 항목과 일치
expression : ma?n
<string>
mn : 1match
man : 1match
maaan : no match (a문자가 2개 이상임)
main : no match
woman : 1matchq
중괄호 "{}" : {n,m}은 최소한 n, 최대 m의 패턴 반복
expression : a{2,3}
<string>
abc dat : no match
abc daat : 1 match
aabc daaat : 2match (aabc , daaat)
aabc daaaat : 2match (aabc , daaaat)
-[0-9] {2, 4} : 2자리 이상 4자리 이하 일치
expression : [0-9] {2, 4}
<string>
ab123csde : 1match (ab123csde)
12 and 345673 : 3match (12 and 345673)
1 and 2 : no match
그룹 : "()" : 하위 패턴을 그룹화하는 데 사용
expression : (a|b|c)xz
<string>
ab xz : no match
abxz : 1match
axz cabxz : 2match
백슬래시 "\' : 모든 메타 문자를 포함한 다양한 문자를 이스케이프하는 데 사용
\$a는 문자열에 $ 다음 'a'가 있으면 일치
'\A'는 지정된 문자가 문자열의 시작 부분에 있으면 일치
expression : \Athe
<string>
the sun : match
in the sun : no match
'\z'는 지정된 문자가 문자열 끝에 있는 경우 일치
expression : python\z
<string>
i like python : 1match
i like programming : no match
'\b'는 지정된 문자가 단어의 시작 또는 끝에 있는 경우 일치
(단어 경계 (공백)
'\B'는 지정된 문자가 단어의 시작 또는 끝에 있지 않은 경우 일치
expression : \bfoo
<string>
football : match
a football : match (단어 경계는 공백이므로)
afootball : no match
expression : foo\b
the foo : match
the afoo test : match
the afoo test: no match
'\d"는 모든 10진수와 일치 : [0-9]와 동일
'\D"는 십진수가 아닌 모든 숫자와 일치
expression : \d
<string>
12abc3 : 3match(12 , 3)
python : no match
'\s"는 문자열에 공백 문자가 포함된 위치와 일치
"\S"와 같음 : "\s"와 반대
expression : \s
<string>
python regex : 1match
pythonregex : no match
"\w"는 모든 영-숫자 문자(숫자 및 알파벳)와 일치
[a-zA-z0-9_]와 동일
밑줄 '_' 도 영-숫자 문자로 간주 됨
expression : \w
<string>
12&": ;c : 3match(12 , c)
%"> ! :no match
정규식 요약
RegEx 모듈
.compile(pattern) : 정규식 패턴 객체 컴파일
.findall(patten, str) : 모든 일치 항목을 포함하는 목록을 반환
.split(pattern , str) : 문자열이 일치할 때마다 분활된 목록을 반환
.sub(pattern, replacement,str) : 하나 이상의 일치 항목을 문자열로 바꿈
findall(pattern, str)
search(patten, str)
split(pattern, str)
sun(pattern, replacement, str)
'파이썬 > 문법' 카테고리의 다른 글
정규표현식 re 모듈 (1) | 2023.11.25 |
---|---|
numpy - vstack과 hstack 함수 (0) | 2023.11.13 |
(파이썬)class 개념 이해 (0) | 2023.09.17 |