C.W.K.
Stream
Lesson 04 of 10 · published

Positive Lookbehind — (?<=...)

~8 min · lookbehind, positive

Level 0패턴 호기심
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

뭔가 앞에 옴 주장, 포함 없이

(?<=...) 는 lookahead 의 거울: "현재 위치에서, lookbehind 안의 패턴이 이 위치 *바로 앞* 텍스트 매칭." 같은 zero-width semantic — 매칭된 텍스트가 캡처에 안 들어감.

고전 사용: 마커 뒤 값 추출

'$' 뒤 가격 추출: (?<=\$)\d+(\.\d+)?. Lookbehind 가 '$' 앞에 있다 주장; 실제 매칭은 숫자만. 없으면 '$19.99' 캡처하고 코드에서 '$' 슬라이스.

같은 패턴이 어떤 prefix 마커든 동작: 'user_' 뒤 사용자 ID 추출, 'on ' 뒤 날짜 추출, X 안 포함하고 X-뒤-거 추출.

너비 제약 (HUGE)

대부분 엔진이 lookbehind 패턴을 fixed-width 로 요구 — 안의 모든 대안이 같은 글자 수 매칭해야.

  • Java, .NET, Python re: Fixed-width 만. (?<=cat) 동작 (3 글자); (?<=cat|elephant) 실패 또는 잘못 동작 가능.
  • Python regex 모듈, PCRE 8+: Variable-width 허용.
  • JavaScript: ES2018 부터 variable-width.
  • RE2 / Go: Lookbehind 아예 미지원.

이게 정규식에서 가장 분열된 기능. Lookbehind 의존하기 전에 항상 본인 엔진 docs 확인.

Code

Lookbehind 패턴·python
import re

# '$' 뒤 가격 추출
re.findall(r'(?<=\$)\d+(?:\.\d+)?', 'paid $19.99 not 20 but $5')
# ['19.99', '5']

# 'user_' 뒤 ID 추출
re.findall(r'(?<=user_)\w+', 'user_alice signed in, user_bob too')
# ['alice', 'bob']

# 'on ' 뒤 날짜 (fixed-width 패턴으로)
re.findall(r'(?<=on )\d{4}-\d{2}-\d{2}', 'logged on 2026-05-04 received')
# ['2026-05-04']

# Variable-width — Python 의 third-party 'regex' 모듈에서 동작
# import regex
# regex.findall(r'(?<=cat|elephant) sat', 'the cat sat the elephant sat')
# ['sat', 'sat']  — 동작

# Python 내장 're' 에선 에러 또는 의외 동작 가능

External links

Exercise

Apache 로그 줄에서 'GET ' 뒤 오는 모든 URL 경로 추출. 경로는 두 번째 필드. Lookbehind 로 'GET ' 에 anchor, 경로 자체 매칭. 샘플 로그 줄 테스트.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.