대부분 로그가 줄 지향, 모양 안정
그게 정확히 정규식의 sweet spot. 각 줄이 같은 템플릿 따름; named 캡처 그룹이 필드 추출.
Apache/nginx Combined Log Format
192.168.1.1 - - [04/May/2026:14:32:11 +0000] "GET /api/data HTTP/1.1" 200 1234
패턴:
(?P<ip>[\d.]+) - - \[(?P<date>[^\]]+)\] "(?P<method>\w+) (?P<path>[^ ]+) [^"]+" (?P<status>\d+) (?P<bytes>\d+)
앱별 구조 로그
2026-05-04T14:32:11Z [INFO] Server started on port 8000 같은 줄:
(?P<timestamp>\S+)\s+\[(?P<level>\w+)\]\s+(?P<message>.+)
Verbose 모드 보상
진짜 로그 패턴이 빨리 길어짐. VERBOSE 모드 + named 그룹이 maintainable 만듦:
LOG = re.compile(r'''
(?P<timestamp>\d{4}-\d{2}-\d{2}T[\d:.]+Z?)
\s+\[(?P<level>\w+)\]
\s+(?P<module>[\w.]+)
\s+(?P<message>.+)
''', re.VERBOSE)
스트리밍 패턴
큰 로그 파일은 줄 단위 스트림 + 각각에 정규식 적용:
with open('big.log') as f:
for line in f:
m = LOG.match(line)
if m:
handle(m.groupdict())
re.compile 결합으로 multi-gigabyte 로그에도 빠름.