정규표현식 (Regular Expression or Regex)
* 공식 doc
https://docs.python.org/3/library/re.html?highlight=re#module-re
import re
pattern & strings
re.match(pattern, strings) : to check if a string matches a specific pattern.
더보기
pattern = r"hello"
text = "hello world"
match = re.match(pattern, text)
if match:
print("Pattern found!")
re.search(pattern, strings) : to find the first occurrence of a pattern in a string.
더보기
pattern = r"world"
text = "hello world"
match = re.search(pattern, text)
if match:
print("Pattern found!")
re.findall(pattern, strings) : to find all occurrences of a pattern
더보기
pattern = r"\d+" # Matches one or more digits
text = "There are 123 apples and 456 oranges."
matches = re.findall(pattern, text)
print(matches) # Output: ['123', '456']
re.split(pattern, strings) : to split text based on a pattern
더보기
pattern = r"\s+" # Matches one or more whitespace characters
text = "Hello world"
parts = re.split(pattern, text)
print(parts) # Output: ['Hello', 'world']
re.sub(pattern, strings) : to replace occurrences of a pattern
더보기
pattern = r"\d+" # Matches one or more digits
text = "There are 123 apples and 456 oranges."
replaced_text = re.sub(pattern, "X", text)
print(replaced_text)
# Output: "There are X apples and X oranges."
() : to group parts of a pattern
더보기
pattern = r"(\d{3})-(\d{2})-(\d{4})" # Matches a date in the format XXX-XX-XXXX
text = "Date: 555-12-3456"
match = re.search(pattern, text)
if match:
print("Year:", match.group(3)) # Output: "Year: 3456"
https://colab.research.google.com/drive/1emcK8PVuzYgNiQJtgTg602_no3jtUln3?usp=sharing
# konlpy
#### 기타
https://github.com/openai/whisper
'Upstage AI Lab 2기' 카테고리의 다른 글
Upstage AI Lab 2기 [Day010] (1) Flask 기초 (0) | 2023.12.26 |
---|---|
Upstage AI Lab 2기 [Day008] (2) 실습 (0) | 2023.12.22 |
Upstage AI Lab 2기 [Day005] 파이썬 AI/데이터 분석 과정 part2. 크롤링 실습 (0) | 2023.12.18 |
Upstage AI Lab 2기 [Day003] 파이썬 AI/데이터 분석 과정 part.1 (2) 파이썬 자료형 (0) | 2023.12.14 |
Upstage AI Lab 2기 [Day003] 파이썬 AI/데이터 분석 과정 part.1 (1) intro (0) | 2023.12.13 |