C.W.K.
Stream
Lesson 12 of 12 · published

정규식 퍼즐 — Mastery 테스트

~15 min · puzzles, practice, graduation

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

퍼즐 다섯, 난이도 증가

다섯 다 풀면 이 quest 박힘. 각각 머리에서 먼저 시도; candidate 가진 후에만 바닥의 답 체크.

퍼즐 1: 길이 3-5 회문 매칭

앞뒤로 같게 읽히는 단어: 'mom', 'eye', 'level', 'civic'. Word boundary 사용.

퍼즐 2: 'fizz' 매칭, 'buzz' 앞에 안 옴

텍스트에서 'fizz' 찾되 (optional 공백 있는) 'buzz' 가 바로 앞에 오는 등장 스킵.

퍼즐 3: 정확히 두 모음 가진 문자열 매칭

'cat' (1), 'beat' (2), 'beats' (2), 'beautiful' (5). 'beat' 와 'beats' 만 매칭.

퍼즐 4: ANSI 색 코드 strip

터미널 출력에 종종 \x1b[31m 또는 \x1b[1;32m 같은 코드. 다른 콘텐츠 제거 없이 다 제거.

퍼즐 5: 인접 중복 단어 찾기

'the the cat' 또는 'sat sat' 같은 거 감지. 중복 단어 반환.

스포일러 (시도 후)

1. \b(\w)(\w?)(\w)?\3\2?\1\b — 중심 주변 쌍 캡처, backreference 와 optional 매칭 사용.

2. (?<!buzz\s?)fizz — negative lookbehind. (Variable-width lookbehind 필요; Python regex 모듈 또는 fixed-width pad.)

3. ^[^aeiou]*[aeiou][^aeiou]*[aeiou][^aeiou]*$ — 비-모음, 모음, 비-모음, 모음, 비-모음.

4. \x1b\[[\d;]*m — escape, 대괄호, 숫자와 세미콜론, m.

5. \b(\w+)\s+\1\b — 반복 감지 backreference.

졸업

다섯 다 이해했으면 — 트레이드오프 (퍼즐 2 의 lookbehind variability, 퍼즐 1 의 중심 글자 처리) 포함 — 졸업. 나머지가 연습. regex101 북마크 유지, lookaround 를 기본 아닌 power tool 로 처리, 황금 규칙 기억: 모양 문제 푸는 가장 작은 도구.

Code

퍼즐 솔루션·python
import re

# 퍼즐 1: 3-5 글자 회문
PALINDROME = re.compile(r'\b(\w)(\w?)(\w)?\3?\2?\1\b')
for m in PALINDROME.finditer('mom eye level civic noon racecar test'):
    word = m.group()
    if word == word[::-1] and 3 <= len(word) <= 5:
        print(word)
# mom
# eye
# level
# civic
# noon

# 퍼즐 2: 'buzz' 앞 안 오는 'fizz'
# Python re 가 fixed-width lookbehind 필요
re.findall(r'(?<!buzz )fizz', 'buzz fizz solo fizz buzzfizz')
# ['fizz', 'fizz']  — 중간과 끝

# 퍼즐 3: 정확히 두 모음
TWO_VOWELS = re.compile(r'^[^aeiou]*[aeiou][^aeiou]*[aeiou][^aeiou]*$')
for word in ['cat', 'beat', 'beats', 'beautiful']:
    if TWO_VOWELS.fullmatch(word):
        print(word)
# beat
# beats

# 퍼즐 4: ANSI 코드 strip
ANSI = re.compile(r'\x1b\[[\d;]*m')
clean = ANSI.sub('', '\x1b[31mhello\x1b[0m \x1b[1;32mworld\x1b[0m')
print(clean)  # 'hello world'

# 퍼즐 5: 인접 중복 단어
DUPS = re.compile(r'\b(\w+)\s+\1\b')
for m in DUPS.finditer('the the cat sat sat by'):
    print(m.group(1))
# the
# sat

External links

Exercise

스포일러 안 보고 다섯 퍼즐 다 풀기. 각각 candidate 정규식 작성, 예시 입력 테스트, peek 전 검증. 첫 시도에 4/5 풀면 regex-master 업적 획득.

Progress

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

댓글 0

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

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