https://docs.python.org/3/tutorial/stdlib.html#string-pattern-matching

re 모듈 핵심 정리와 간단 팁:

  • 패턴 매칭: re.findall(r’\bf[a-z]*’, text) → 단어 경계에서 f로 시작하는 소문자 단어 찾기.
  • 치환: re.sub(r’(\b[a-z]+) \1’, r’\1’, text) → 같은 단어가 두 번 반복되면 하나로 축약.

자주 쓰는 함수:

  • re.search(pattern, text): 첫 매치(어디든).
  • re.match(pattern, text): 문자열 시작에서만 매치.
  • re.fullmatch(pattern, text): 전체가 패턴과 일치할 때만.
  • re.findall(pattern, text): 모든 매치 리스트.
  • re.finditer(pattern, text): 매치 이터레이터(위치 포함).
  • re.sub(pattern, repl, text): 치환.
  • re.split(pattern, text): 패턴 기준 분리.

플래그 예:

  • re.I(IGNORECASE), re.M(MULTILINE ^, $), re.S(DOTALL .가 줄바꿈 포함), re.X(VERBOSE: 패턴에 공백/주석 허용).
  • 예: re.findall(r’^\w+’, text, flags=re.M)

그룹/참조:

  • 캡처: (…), 비캡처: (?:…)
  • 그룹 참조: \1, \2 …; 이름 있는 그룹: (?P…), 참조 (?P=name)
  • 치환에서 그룹 사용: re.sub(r’(\w+), (\w+)’, r’\2 \1’, “Doe, John”)

탐욕/비탐욕:

  • , +, ?는 기본 탐욕; 뒤에 ? 붙이면 비탐욕: .?, +?

경계/앵커:

  • ^, $ (멀티라인 플래그에 따라 의미 변함), \b 단어 경계, \B 비-단어 경계.

간단한 문자열 처리로 충분하면 내장 메서드(replace, split, in, startswith, endswith)가 더 읽기 쉽고 디버깅이 쉽습니다.

← python 3.14으로