Python

re.error: look-behind requires fixed-width pattern

xojuholic 2024. 1. 24.
728x90
반응형

re.error

 

아래는 정규표현식에서 '후방 탐색(look-behind)' 시도하다 발생한 error 코드이다.

# 2. Substitution (교체)
str3 = "010-2343-4444"
result = re.sub("(?<=\d{3}-\d{3,4}-)\d{4}", "****", str3)

 

그리고 아래는 error를 잡은 코드이다.

# 2. Substitution (교체)
str3 = "010-2343-4444"
result = re.sub("(?<=\d{3}-\d{4}-)\d{4}", "****", str3)

 

차이점은 범위를 고정(fix)[-\d{4}-]했음을 알 수 있다. '전방 탐색(look-ahead)'에서는 범위를 고정하지 않아도 되나 '후방 탐색(look-behind)'에서는 범위를 고정해야 한다.

728x90
반응형
SMALL

댓글

💲 추천 글