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

Cleanup 패턴

~8 min · cleanup, normalization

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

흔한 cleanup, 가져갈 준비됨

텍스트 정규화가 정규식 빛나는 자리. 각각이 복사 가능한 한 줄.

공백 collapse

re.sub(r'\s+', ' ', text) — 공백 (스페이스, 탭, 줄바꿈) 의 어떤 run 이든 단일 공백으로 교체.

비-printable 글자 strip

re.sub(r'[^\x20-\x7E\n]', '', text) — printable ASCII + 줄바꿈만 유지. 손상된 텍스트 정리에 유용.

HTML 태그 제거 (loose, 파싱 X)

re.sub(r'<[^>]+>', '', text) — 꺽쇠 안 모든 거 strip. Ad-hoc 텍스트 추출엔 OK. 진짜 HTML 엔 파서.

줄당 trailing 공백 trim

re.sub(r'[ \t]+$', '', text, flags=re.MULTILINE) — 각 줄에서 trailing 공백과 탭 제거.

줄바꿈 정규화

re.sub(r'\r\n?', '\n', text) — Windows (\r\n) 와 옛 Mac (\r) 줄바꿈을 Unix (\n) 로 변환.

둘러싼 따옴표 strip

re.sub(r'^["\']+|["\']+$', '', text) — 앞뒤 따옴표 제거.

민감 패턴 마스킹

re.sub(r'\b\d{4}-\d{4}-\d{4}-\d{4}\b', '****-****-****-****', text) — 신용카드-shape 문자열 마스킹. 같은 패턴, 다른 치환, SSN 등에.

제목 slugify

3 단계: 소문자, 비-영숫자를 하이픈으로 교체, 다중 하이픈 collapse.

Code

Cleanup 패턴·python
import re

# 공백 collapse
re.sub(r'\s+', ' ', 'too\tmany\n\nspaces')
# 'too many spaces'

# 비-printable strip
re.sub(r'[^\x20-\x7E\n]', '', 'hello\x00world\x07')
# 'helloworld'

# HTML 태그 제거
re.sub(r'<[^>]+>', '', '<p>Hello <b>world</b></p>')
# 'Hello world'

# Trailing 공백 trim
re.sub(r'[ \t]+$', '', 'line1   \nline2 \t', flags=re.MULTILINE)
# 'line1\nline2'

# 줄바꿈 정규화
re.sub(r'\r\n?', '\n', 'win\r\nmac\rmix\n')
# 'win\nmac\nmix\n'

# 신용카드 마스킹
re.sub(r'\b\d{4}-\d{4}-\d{4}-\d{4}\b', '****-****-****-****', 'card 1234-5678-9012-3456')
# 'card ****-****-****-****'

# Slugify
def slugify(text):
    text = text.lower()
    text = re.sub(r'[^a-z0-9]+', '-', text)
    text = re.sub(r'-+', '-', text)
    return text.strip('-')

slugify('Hello, World! Pippa #1')
# 'hello-world-pippa-1'

External links

Exercise

혼합 cleanup 필요 (추가 공백, 이상한 글자, unicode 따옴표, 줄 끝) 텍스트 파일 잡기. Cleanup 패턴 3-4개 순차 적용. before/after 비교. Production 텍스트 파이프라인이 이렇게 생김 — 작은 정규식의 chain.

Progress

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

댓글 0

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

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