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

코드 패턴 — 식별자와 주석

~8 min · code-mods, identifiers

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

식별자

대부분 언어: 문자 또는 underscore, 그 다음 문자/숫자/underscore.

[a-zA-Z_]\w*

네이밍 컨벤션엔:

  • camelCase: [a-z][a-zA-Z0-9]*
  • snake_case: [a-z][a-z0-9_]*
  • SCREAMING_SNAKE: [A-Z][A-Z0-9_]*
  • PascalCase: [A-Z][a-zA-Z0-9]*
  • kebab-case: [a-z][a-z0-9-]*

주석

Single-line C 식: //[^\n]* 또는 anchor 된 ^//.*$ + MULTILINE.

Single-line shell/Python: #[^\n]* (false positive 주의 — URL 안 #, 문자열 안).

Multi-line C 식: /\*[\s\S]*?\*/ — 주의 [\s\S], . 대신, DOTALL 플래그 없이 줄바꿈 가로지름.

진짜 주석 감지엔 토크나이저 필요 — 문자열 안 주석-like 패턴이 false positive. 정확성 중요한 소스 코드 처리엔 진짜 파서/AST.

Code mod

정규식이 파일 가로지른 리팩토링에 빛남: 함수 rename, import 변경, config 블록 재구조화. One-shot mod 엔 VS Code 의 정규식 find/replace 또는 sed. 복잡한 리팩토링엔 AST 도구 (Python libcst, JS jscodeshift) 가 더 안전.

Code

코드 정규식 예시·python
import re

# 식별자 패턴
IDENT = re.compile(r'[a-zA-Z_]\w*')
re.findall(IDENT, 'function getUserById(user_id) {}')
# ['function', 'getUserById', 'user_id']

# camelCase 만
CAMEL = re.compile(r'\b[a-z][a-zA-Z0-9]*\b')
re.findall(CAMEL, 'getUserById vs get_user_by_id')
# ['getUserById', 'get', 'user', 'by', 'id']  — snake_case 분리

# Multi-line C 식 주석
COMMENT = re.compile(r'/\*[\s\S]*?\*/')
re.findall(COMMENT, '/* hello */ code /* world */')
# ['/* hello */', '/* world */']

# JavaScript 의 import 찾기
IMPORT = re.compile(
    r'^import\s+(?:\{[^}]+\}|\*\s+as\s+\w+|\w+)\s+from\s+[\'"]([^\'"]+)[\'"]',
    re.MULTILINE
)
code = '''
import React from 'react';
import { useState } from 'react';
import * as fs from 'fs';
'''
re.findall(IMPORT, code)
# ['react', 'react', 'fs']

External links

Exercise

본인 코드베이스의 함수 하나 골라. VS Code 정규식 find/replace 로 한 패스에 모든 호출자 가로질러 rename. False positive (주석 안 등장, 비슷한 이름 식별자) 인지. 결정: 정규식 충분, AST 필요?

Progress

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

댓글 0

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

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