메타 문자란 원래 그 문자가 가진 뜻이 아니라 특별한 의미를 가진 문자를 의미,
정규 표현식에 다음과 같은 메타 문자를 사용하면 특별한 의미를 가지게 된다
- . ^ $ * + ? { } [ ] \ | ( )
* [] : 문자클래스를 만드는 메타 문자, 어떤 문자도 들어갈 수 있다.
ex) [abc] 라면 a,b,c 중 한개의 문자와 매치를 의미한다
before 은 b가 있으므로 매치
dude 는 abc 중 어느 하나도 없으므로 매치 x
[] 안에 - 하이픈을 사용하면 범위를 나타낸다 a-c = abc 이다
* ^와 $ → 문자열의 시작과 끝
def is_english(text):
pattern = r"^[A-Za-z]+$" # 영어만 허용
return bool(re.match(pattern, text))
* 공백을 포함해도 되려면 \s 추가
def is_english_with_space(text):
pattern = r"^[A-Za-z\s]+$" # 영어 + 공백 허용
return bool(re.match(pattern, text))
* .(dot) \n 을 제외한 모든 문자와 매치
ex) a.b 는 a + 모든문자 + b 를 의미한다. 즉 a와 b 사이에 어떤 글자가 들어가도 매치
pattern = r"a.b" # a와 b 사이에 어떤 문자든 하나만 있으면 매치
print(bool(re.search(pattern, "acb"))) # True ('c'가 사이에 있음)
print(bool(re.search(pattern, "ab"))) # False (중간에 아무것도 없음)
* {} 반복 문자 고정
ex)
ca{2}t c + a를 반드시 2번 반복 + t
cat 만 매치
pattern = r"ca{2}t" # 'c' + 'a'가 2번 + 't'
print(bool(re.search(pattern, "caat"))) # True
print(bool(re.search(pattern, "cat"))) # False ('a'가 1번만 등장)
* ca{2,5}t 2~5회 반복시 매치
pattern = r"ca{2,5}t" # 'a'가 2~5번 반복
print(bool(re.search(pattern, "caaat"))) # True ('a'가 3번)
print(bool(re.search(pattern, "cat"))) # False ('a'가 1번만 등장)
* \d : 숫자를 의미하며,[0-9]와 동일
pattern = r"\d" # 숫자가 포함되어 있으면 매치
print(bool(re.search(pattern, "hello123"))) # True (숫자 포함)
print(bool(re.search(pattern, "hello"))) # False (숫자 없음)
* + : 바로앞의 패턴이 하나 이상 연속으로 반복됨을 의미
pattern = r"a+" # 'a'가 1개 이상 반복되면 매치
print(bool(re.search(pattern, "aaaa"))) # True
print(bool(re.search(pattern, "b"))) # False ('a' 없음)
* * : 무한대로 반복될수 있다는 의미
pattern = r"ab*c" # 'a' + ('b'가 0개 이상) + 'c'
print(bool(re.search(pattern, "ac"))) # True ('b' 없이 매치 가능)
print(bool(re.search(pattern, "abbc"))) # True ('b' 여러 개 가능)
print(bool(re.search(pattern, "adc"))) # False ('b'가 아닌 다른 문자가 중간에 있음)
import re
print(re.match('a','ab')) # 매치
print(re.match('a','bba')) # b로시작하여 매치 실패
import re
print(re.search('a','ab')) # 매치
print(re.search('a','bba')) # 매치
def validate_password(cls, value):
if len(value) < 8:
raise ValueError("Password must be at least 8 characters long")
if not re.search(r"[A-Z]", value):
raise ValueError("Password must contain at least one uppercase letter")
if not re.search(r"\d", value):
raise ValueError("Password must contain at least one number")
if not re.search(r"[!@#$%^&*(),.?\":{}|<>]", value):
raise ValueError("Password must contain at least one special character")
return value
print(re.findall('a','a')) # ['a']
print(re.findall('aaa','aaaaa')) # ['aaa']
print(re.findall('aaa','aaaaaa')) # ['aaa','aaa']
print(re.findall('a','baa')) # ['a','a']
print(re.findall('\d','숫자 123이 이렇게 56 있다8'))
# ['1','2','3','5','6','8']
print(re.findall('\d+', 숫자 123이 이렇게 56 있다8'))
# ['123','56','8']
자료형을 도와주는 모듈
from collections import Counter
lst = ['aa', 'cc', 'dd', 'aa', 'bb', 'ee']
print(collections.Counter(lst)) # 쓰려면 변수로 선언
# Counter({'aa': 2, 'cc': 1, 'dd': 1, 'bb': 1, 'ee': 1}) 출력
from collections import defaultdict
int_dict = defaultdict(int)
int_dict['key1'] # key1 이라는 key 갑생성 벨류는 안넣음 = 0
int_dict['key2'] = 'test' # key2 : test
from collections OrderedDict
ex1 = OrderedDict.fromkeys(x) # 중복 제거
from collections import namedtuple
Point = namedtuple(Point,'x y')
pt = Point(1.0,3.0) x = 1.0, y = 3.0
import heapq
heap = []
heapq.heappush(heap, 50) #
heap2 = [50 ,10, 20]
heapq.heapify(heap2) # 이미 존재하는 리스트 힙화
result = heapq.heappop(heap) # 삭제 후 반환
print(heap)
heap_items = [1,3,5,7,9]
max_heap = []
for item in heap_items:
heapq.heappush(max_heap, (-item, item))
print(max_heap)
코드 재사용 및 최적화를 위해 사용하는 모듈
from functools import partial
def multiply(x, y) :
return x * y
# y 값을 2로 고정한 새로운 함수 생성
double = partial(multiply, y=2)
print(double(5)) # 10
print(double(7)) # 14
from functools import lru_cache
import time
@lru_cache(maxsize=None)
def slow_function(n) :
time.sleep(2) # 계산이 오래 걸리는 함수 시뮬레이션
return n * n
start = time.time()
print(slow_function(4)) # 처음 호출 , 2초지연
print(time.time() - start)
stat = time.time()
print(slow_function(4)) # 캐시된 결과 사용 , 지연 없음 !!,전결과 저장했다 사용하는것
print(time.time() - start)
slow_function.cache_clear() # 캐싱 저장 삭제
def keyfunc():
pass
key_func = cmp_to_key(keyfunc)
sorted_x = sorted(x, key = key_func)
from functools import reduce
numbers = [1,2,3,4,5]
# 누적 합계 계산
total = reduce(lambda x, y : x+y, numbers)
print(total) # 15
from functools import total_ordering
@total_ordering
class Person :
def __init__(self,name,age) :
self.name = name
slef.age = age
def __eq__(self, other) :
if isinstance(other, Person) :
return self.age == other.age
return NotImplemented
def __lt__(self, other) :
if isinstance(other, Person) :
return self.age < other.age
return NotImplemented
def __repr__(self) :
return f'Person(name = {self.name}, age = {self.age}'
person1 = Person('Chill', 30)
person2 = Person('Bob', 25)
person3 = Person('Hoon', 30)
print(person1 == person2) # False
print(person1 == person3) # True
print(person1 < person2) # False
print(person2 < person1) # True
print(person1 > person2) # True
효율적인 루핑을 위한 이터레이터를 만드는 함수
from itertools import combinations
list = [1, 2, 3]
for i in combinations(list, 2) : # 3C2
print(i)
print(list(map(''.join, combinations(list,2))))
(1, 2)
(1, 3)
(2, 3)
[(1, 2), (1, 3), (2, 3)]
from itertools import permutations
list = ['A', 'B', 'C']
for i in permutations(list): #r을 지정하지 않거나 r=None으로 하면 최대 길이의 순열이 리턴된다.
print(i)
print(list(map(''.join, itertools.permutations(list))))
print(list(map(''.join, itertools.permutations(list, 2)))) #2개의 원소로 수열 만들기
('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']
['AB', 'AC', 'BA', 'BC', 'CA', 'CB']
파이썬 인터프리터를 제어하는데 사용되는 기본 모듈
from datetime import datetime, timezone
now = datetime.now()
utc_now = datetime.utcnow()
utc_now = datetime.now(timezone.utc) # 타임존 정보 포함
custom_date = datetime(2025, 2, 17, 15, 30, 45)
# 2025-02-17 15:30:45
now = datetime.now()
format_date = now.strftime("%Y-%m-%d %H:%M:%S")
# "2025-02-17 15:30:00"
# %Y: 연도(4자리), %y: 연도(2자리)
# %H: 시(24시간제), %I: 시(12시간제)
# %p: AM/PM
date_str = "2025-02-17 15:30:00"
date_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
# 2025-02-17 15:30:00
from datetime import timedelta
today = datetime.now()
one_month_later = today + timedelta(months=1)
# 현재 날짜 + 1달 후
three_days_later = today + timedelta(days=3)
# 현재 날짜 + 3일 후
one_week_ago = today - timedelta(weeks=1)
# 현재 날짜 - 1주 전
date1 = datetime(2025, 2, 17)
date2 = datetime(2025, 3, 1)
diff = date2 - date1
print(diff.days)
today = date.today()
print(today) # 2025-02-17
now = datetime.now()
timestamp = now.timestamp() # 현재 시간을 Unix Timestamp로 변환
print(timestamp) # 1739784600.123456
# 타임스탬프 -> 날짜
date_from_timestamp = datetime.fromtimestamp(timestamp)
print(date_from_timestamp) # 2025-02-17 15:30:00
today = datetime.today()
print(today.weekday()) # 0 (월요일: 0, 일요일: 6)
from datetime import timezone
utc_now = datetime.now(timezone.utc) # UTC 기준 시간
local_now = utc_now.astimezone() # 시스템 로컬 시간으로 변환
print(local_now)
import uuid
uuid_4 = uuid.uuid4()
print(uuid_4) # 예: 2a737d89-f74b-4c45-9e41-5d369e1f5e56
uuid_3 = uuid.uuid3(uuid.NAMESPACE_DNS, "example.com")
uuid_5 = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")
print(uuid_3) # 예: 5df41881-3aed-3515-88a7-2f4a814cf09e
print(uuid_5) # 예: 2ed6657d-e927-568b-95e1-2665a8aea6a2
uuid_str = str(uuid.uuid4())
print(uuid_str) # 예: '2a737d89-f74b-4c45-9e41-5d369e1f5e56'
uuid_bytes = uuid.uuid4().bytes
print(uuid_bytes) # 예: b'\x9e\x18\xd7\xcdK\x94J\xcf\x98\xcf\xf3\xc0\x10\xe8\xb1\x12'
import random
print(random.randint(1, 10)) # 1~10 사이의 랜덤 정수 (포함)
print(random.randrange(1, 10, 2)) # 1~9 사이에서 2 간격으로 선택 (예: 1, 3, 5, 7, 9 중 하나)
print(random.random()) # 0.0 ~ 1.0 사이의 랜덤 실수
print(random.uniform(1.5, 6.5)) # 1.5 ~ 6.5 사이의 랜덤 실수
colors = ["red", "blue", "green", "yellow"]
print(random.choice(colors)) # 리스트에서 랜덤 요소 1개 선택
print(random.choices(colors, k=2)) # 리스트에서 중복 가능하게 2개 선택
print(random.sample(colors, k=2)) # 리스트에서 중복 없이 2개 선택
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers) # 리스트 순서 랜덤 변경
random.seed(42) # 랜덤 시드 고정
print(random.randint(1, 10)) # 항상 같은 결과가 나옴
print(random.random()) # 실행할 때마다 동일한 랜덤 값
- 운영체제와 상호작용하는 기능
- 파일 시스템 조작, 환경 변수 접근, 프로세스 관리 등에 사용..
import os
# 현재 작업 디렉토리 확인 및 변경
print(os.getcwd()) # 현재 작업 디렉토리 출력
os.chdir('/path/to/directory') # 작업 디렉토리 변경
# 파일 및 폴더 생성, 삭제
os.mkdir('new_folder') # 새로운 디렉토리 생성
os.makedirs('parent_folder/child_folder') # 부모 폴더 포함하여 디렉토리 생성
os.rmdir('new_folder') # 빈 디렉토리 삭제
os.removedirs('parent_folder/child_folder') # 상위 폴더까지 삭제
os.remove('file.txt')
# 파일 및 폴더 존재 여부 확인
os.path.exists('file.txt') # 파일 또는 폴더가 존재하는지 확인 (True/False)
os.path.isfile('file.txt') # 파일인지 확인
os.path.isdir('my_folder') # 폴더인지 확인
# 디렉토리 내 파일 리스트 가져오기
print(os.listdir('.')) # 현재 디렉토리의 파일 목록 출력
# 경로 합치기
path = os.path.join('folder', 'subfolder', 'file.txt')
print(path) # 'folder/subfolder/file.txt' (운영체제에 따라 다름
# 절대 경로 변환
print(os.path.abspath('file.txt')) # 절대 경로 출력
# 경로 분리
path = '/home/user/docs/file.txt'
print(os.path.dirname(path)) # '/home/user/docs'
print(os.path.basename(path)) # 'file.txt'
# 파일 확장자 확인
filename, extension = os.path.splitext('document.pdf')
print(filename) # 'document'
print(extension) # '.pdf'
print(os.environ['HOME']) # 사용자 홈 디렉토리
os.environ['MY_VAR'] = '123' # 환경 변수 설정
print(os.getenv('MY_VAR')) # 환경 변수 가져오기 (없으면 None 반환)
# 터미널 명령어 실행(os.system)
os.system('ls -l') # UNIX: 파일 목록 출력
os.system('dir') # Windows: 파일 목록 출력
# 명령 실행 결과 가져오기 (os.popen)
output = os.popen('ls').read()
print(output) # 파일 목록 출력
# 현재 프로세스 ID 확인
print(os.getpid()) # 현재 실행 중인 프로세스 ID 출력
# 부모 프로세스 ID 확인
print(os.getppid()) # 부모 프로세스 ID 출력
# 사용자 ID 확인
print(os.getlogin()) # 현재 로그인한 사용자 이름 출력
print(os.name) # 현재 OS 종류 ('posix' (Linux/macOS) 또는 'nt' (Windows))
print(os.uname()) # OS 상세 정보 (Linux/macOS 전용)