python 메모

[python] regex 메모

2021. 5. 7. 21:41
목차
  1. 1) re.split()
  2. 2) re.group()
  3. 3) re.sub()
  4. 4) findall()

regexr.com의 regex cheatsheet은 여기가 유용하다.

 

1) re.split()

특정 기호 그룹으로 문자열을 나눌 때 사용한다

# input: '100,200,300.400'
# output: '100 200 300 400'

import re

regex_pattern = r"[,.]"
s = '100,200,300.400'

' '.join(re.split(regex_patter, s))

 

2) re.group()

regex pattern에 매치되는 문자열 리스트를 얻고 싶을 때 사용한다.

import re

s = 'useful@python.codes'
m = re.match(r'(\w+)@(\w+)\.(\w+)', s)

print(m)
>>> <_sre.SRE_Match object; span=(0, 19), match='useful@python.codes'>

m.groups()
>>> ('useful', 'python', 'codes')

m.group()  # entire match
>>>> 'useful@python.codes'

m.group(0)  # entire match
>>> 'useful@python.codes'

m.group(1)  # first match
>>> (useful)

m.group(1,2,3)
>>> ('useful', 'python', 'codes')

 

re.groupdict()는 매치되는 문자열에 key를 주고 리턴할 때 사용한다.

import re

s = 'useful@python.codes'
m = re.match(r'(?P<user>\w+)\@(?P<website>\w+)\.(?P<extension>\w+)', s)

m.groupdict()
>>>> {'user': 'useful', 'website': 'python', 'extension': 'codes'}

 

group의 중요한 두 가지 기능은 그룹과 캡쳐이다. 자세한 내용은 여기에 설명이 잘 되어 있다.

 

3) re.sub()

(regex pattern, 매치된 문자열에 적용할 함수, 문자열) 입력으로 받는다.

import re

def square(match):
	number = int(match.group(0))
    return str(number**2)
    
print(re.sub(r"\d+", square, "1 2 3 4 5 6 7 8 9"))
>>>> 1 4 9 16 25 36 49 64 81

s = "Can#%you*please@#open*the$#@%door?"
print(re.sub(r"\b[^a-zA-Z0-9?]+\b", r' ', s))
>>>> Can you please open the door?

 

4) findall()

조건에 해당하는 문자열들을 리스트로 리턴해준다.

import re

data = '제 47 기 회계일자는 2015.01.01 부터 2015.12.31 까지이다'

p = re.compile(r'(\d+.\d+.\d+)')
p.findall(data)
>>>> ['2015.01.01', '2015.12.31']
728x90
저작자표시 비영리 동일조건 (새창열림)

'python 메모' 카테고리의 다른 글

[python] First-Class Function과 Closure, Decorator  (0) 2021.06.01
[matplotlib] subplot 그리기  (0) 2021.05.19
[python] 봐두면 유용할 수도 있는 문자열 built-in functions  (0) 2021.05.06
[python] itertools  (0) 2021.05.06
[python] any(), all()  (0) 2021.05.06
  1. 1) re.split()
  2. 2) re.group()
  3. 3) re.sub()
  4. 4) findall()
'python 메모' 카테고리의 다른 글
  • [python] First-Class Function과 Closure, Decorator
  • [matplotlib] subplot 그리기
  • [python] 봐두면 유용할 수도 있는 문자열 built-in functions
  • [python] itertools
Fine애플
Fine애플
이것저것
끄적끄적이것저것
Fine애플
끄적끄적
Fine애플
전체
오늘
어제
  • 분류 전체보기 (167)
    • 논문 및 개념 정리 (27)
    • Pattern Recognition (8)
    • 개발 (57)
    • python 메모 (45)
    • pytorch, tensorflow (5)
    • 알고리즘 (9)
    • Toy Projects (4)
    • 통계이론 (2)
    • Reinforcement Learning (10)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • Bert
  • nlp
  • 개발환경
  • GPU
  • python
  • miniconda
  • pandas
  • Docker
  • 자연어
  • ubuntu
  • reinforcement learning
  • Probability
  • container
  • 언어모델
  • transformer
  • PyTorch
  • BigBird
  • tensorflow
  • 딥러닝
  • 알고리즘

최근 댓글

최근 글

hELLO · Designed By 정상우.
Fine애플
[python] regex 메모
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.