regex

Parsing

고급 정규식 엔진

개요 regex

regex 모듈은 Python의 내장 re 모듈을 즉시 대체하여 퍼지 일치(대략적인 문자열 일치), 가변 너비 뒤돌아보기, 반복 캡처가 포함된 명명된 캡처 그룹, POSIX 문자 클래스 및 원자 그룹화를 포함한 추가 기능을 제공합니다.

정규식은 micropip을 통해PyRun에서 사용할 수 있습니다. 약간의 오타(퍼지 매칭)가 있는 패턴을 일치시켜야 하거나, 복잡한 유니코드 텍스트로 작업하거나, 표준 re 모듈에서 지원되지 않는 정규식 기능을 사용해야 할 때 특히 유용합니다.

코드 및 실행 출력

명명된 캡처 그룹 및 퍼지 패턴 일치.

Regex: Named Groups & Fuzzy에디터에서 실행
import regex

# Named capture groups — parse a log line
log = "2025-07-04 14:32:01 ERROR [auth] Login failed for user@example.com"
pattern = r"(?P<date>\d{4}-\d{2}-\d{2}) (?P<time>\d{2}:\d{2}:\d{2}) (?P<level>\w+) \[(?P<module>\w+)\] (?P<message>.+)"
m = regex.match(pattern, log)
if m:
    for k, v in m.groupdict().items():
        print(f"  {k:<10}: {v}")

# Fuzzy matching — find 'colour' with up to 1 error
print("\nFuzzy search (≤1 substitution):")
text = "I prefer colour and cilor and colouur"
for hit in regex.finditer(r"(?:colour){s<=1}", text):
    print(f"  found '{hit.group()}' at {hit.span()}")

관련 패키지

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.