90% 사용할 네 함수
Python 내장 정규식은 re 모듈에 살아. 네 함수가 대부분 사용 케이스 커버:
re.search(pattern, text)— 텍스트 어디서든 첫 매칭. Match 객체 또는 None 반환.re.match(pattern, text)— 텍스트 시작에서만 매칭. Match 또는 None.re.fullmatch(pattern, text)— 텍스트 전체 매칭. Match 또는 None.re.findall(pattern, text)— 모든 비-overlapping 매칭 리스트로.re.finditer(pattern, text)— Match 객체 iterator (그룹 있을 때 findall 보다 선호).
Match 객체
패턴 매칭하면 Match 객체 받아, 핵심 메서드:
m.group()또는m.group(0)— 전체 매칭m.group(N)— N번째 캡처 그룹m.group('name')— named 캡처 그룹m.groups()— 모든 번호 그룹 튜플m.groupdict()— 모든 named 그룹 dictm.start(),m.end(),m.span()— 매칭 위치
Match vs search — 가장 흔한 혼동
re.match 는 문자열 시작만 체크. re.match(r'world', 'hello world') 가 None 반환. "포함" 은 re.search. "시작" 은 re.match. "정확히" 는 re.fullmatch.