티스토리 뷰

출처 : http://i5on9i.egloos.com/4840659
 

phthon regular expression (regex) module
파이썬 정규 표현식 설명

re.match 와 re.search 의 차이점
출처 : http://www.regular-expressions.info/python.html

re.match 와 re.search 의 차이점
re.match 와 re.search 의 차이점은
re.search 는 주어진 string 전체에서 regex와 맞는 pattern 을 찾아내고,
re.match 는 주어진 string 의 시작부분부터 pattern 비교를 시작한다.
다시 말하면,
re.match('a', "ab") 는 성공하지만
re.match('b', "ab") 는 None 을 반환한다.
언제나 pattern 을 string 의 첫부분과 비교하기 때문이다.
반면에 re.search 는
re.search('a', "ab")
re.search('b', "ab")
둘 다 성공한다. 문자열 내에서 pattern 과 맞는 부분을 다 찾아내기 때문이다.
그래서
re.match("regex", subject) 와 re.search("Aregex", subject) 는 같다.
match 가 string 의 처음시작 부분과 관련된 pattern 을 정의 해야 하지만,
그렇다고 해서 string 전체를 표현하는 regex를 가지고 있을 필요는 없다.

re.S
re.search("^a", "abc", re.I | re.M | re.S).
re.S( re.DOTALL ) 는 '.' 가 new line 까지도 match 하게 해 준다.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함