강좌
클라우드/리눅스에 관한 강좌입니다.
프로그램 분류

파이썬기초70 : 문자열처리 내장함수2

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

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'


관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,034 명
  • 현재 강좌수 :  35,791 개
  • 현재 접속자 :  226 명