최대 1 분 소요

웹스크래핑 만들기 2

  1. 원하는 형태를 받는다
  • .(.ca.e):하나의 문자를 의미한다

  • ^(^de):문자열의 시작을 의미한다

  • $(se$):문자열의 끝을 의미한다

import re

p =re.compile("ca.e")

 

  1. match
  • 주어진 문자열의 처음부터 일치하는지 확인한다 ```python m = p.match(“case”)

print(m.group())


case 출력시 출력

caffee는 출력되지 않음


```python
m = p.match("case")  

if m :
    print(m.group())
else:
    print("매칭되지않음")

case 출력시에는 case 출력

caffee 출력시에는 “매칭되지않음”

def print_match(m):
    if m:
        print(m.group())
    else:
        print("매칭되지 않음")

def print_match(m):
    if m:
        print("m.group()):",m.group()) # 일치하는 문자열을 반환한다
        print("m.string:",m.string)
        #입력받은 문자열을 반환
        print("start()",m.start())
        #일치하는 문자열의 시작 index
        print("m.end():",m.end())
        #일치하는 문자열의 끝 index
        print("m.span():", m.span())
        #일치하는 문자열의 시작/끝 index
    else:
    print("매칭되지 않는다")

 

  1. search

문자열 중에 일치하는게 있는지 확인한다

m = p.search("good care")

print_match(m)

 

  1. findall
  • 일치하는 모든 것을 리스트 형태로 변환한다
lst = p.findall("good care cafe")
print(lst)

카테고리:

업데이트:

댓글남기기