본문 바로가기

Upstage AI Lab 2기

Upstage AI Lab 2기 [Day008] Python project(day2) (작성)

정규표현식 (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