파이썬기초70 : 문자열처리 내장함수2
작성자 정보
- 관리자 작성
- 작성일
컨텐츠 정보
- 1,662 조회
- 0 추천
- 목록
본문
import re
result = re.match('[0-9]*th', '35th')
type(result)
Out[]: _sre.SRE_Match
result
Out[]: <_sre.SRE_Match object; span=(0, 4), match='35th'>
re.search('[0-9]*th', '35th')
Out[]: <_sre.SRE_Match object; span=(0, 4), match='35th'>
bool(re.match('[0-9]*th', ' 35th'))
Out[]: False
bool(re.search('[0-9]*th', ' 35th'))
Out[]: True
bool(re.match('ap', 'This is an apple'))
Out[]: False
bool(re.search('ap', 'This is an apple'))
Out[]: True
re.split('[:, ]+', 'apple Orange:banana tomato')
Out[]: ['apple', 'Orange', 'banana', 'tomato']
re.split('([:, ])+', 'apple Orange:banana tomato')
Out[]: ['apple', ' ', 'Orange', ':', 'banana', ' ', 'tomato']
re.split('[:, ]+', 'apple Orange:banana tomato', 2)
Out[]: ['apple', 'Orange', 'banana tomato']
text = """Gee Gee Gee Gee Baby Baby Baby
Oh I am shy
I can't see you
"""
re.split('\n+', text)
Out[]:
['Gee Gee Gee Gee Baby Baby Baby',
' Oh I am shy',
" I can't see you",
' ']
text1 = "application orange apple banana"
re.findall(r"app\w*", text1)
Out[]:['application', 'apple']
text2 = "901225-1234567"
re.sub("-", "", text2)
Out[]: '9012251234567'
text3 = "Apple:Orange Banana|Tomato"
re.sub(r"[:,|\s]", ", ", text3)
Out[]: 'Apple, Orange, Banana, Tomato'
text4 = "Copyright Derick 1990-2009"
re.sub(r"\b(\d{4}-\d{4})\b", r"<I>\1</I>", text4)
Out[]: 'Copyright Derick <I>1990-2009</I>'
text5 = "The time is the money"
def Upper(m):
return m.group().upper()
re.sub("[T|t]he", Upper, text5)
Out[]: 'THE time is THE money'
text6 = "application orange apple banana"
c = re.compile(r"app\w*")
c.findall(text6)
Out[]: ['application', 'apple']
text7 = "There are so many apples in the basket"
c.findall(text7)
Out[]: ['apples']
telChecker = re.compile(r"(\d{2,3})-(\d{3,4})-(\d{4})")
bool(telChecker.match("02-123-4567"))
Out[]: True
bool(telChecker.match("02-k123-4567"))
Out[]: False
bool(telChecker.match("3402-123-4567"))
Out[]: False
bool(telChecker.match("032-717-1312"))
Out[]: True
bool(telChecker.match("010-3444-4456"))
Out[]:True
m = telChecker.match("031-717-1312")
m.group()
Out[]: '031-717-1312'
m.groups()
Out[]: ('031', '717', '1312')
m.group(1)
Out[]: '031'
m.group(2)
Out[]: '717'
m.group(3)
Out[]: '1312'
m.group(1,2)
Out[]: ('031', '717')
m.group(2,3)
Out[]: ('717', '1312')
m.group(1,3)
Out[]: ('031', '1312')
m.start()
Out[]: 0
m.end()
Out[]: 12
m.start(1)
Out[]: 0
m.end(1)
Out[]: 3
m.start(2)
Out[]: 4
m.end(2)
Out[]: 7
m.start(3)
Out[]: 8
m.end(3)
Out[]: 12
m.string[m.start(1):m.end(2)]
Out[]: '031-717'
m.string[m.start(2):m.end(3)]
Out[]: '717-1312'
m.string[m.start(1):m.end(3)]
Out[]: '031-717-1312'
관련자료
-
이전
-
다음