00 개요
- 코드분석 시 나오길래 정리하고자 함
- re는 regular expression(정규 표현식)의 약자인데 뭔지 알고자 할 경우 다음을 참조 (2024.03.26 - [Python/기본문법] - Regular Expression (정규표현식))
01 re 모듈이란
1. re 정의
- 'Regular Expression'의 약자
- 이 모듈은 정규 표현식 매칭 작업을 제공
- both patterns and strings to be searched can be Unicode strings (str) or 8-bit strings (bytes)
- Unicode strings and 8-bit strings cannot be mixed
- you cannot match/substitute a Unicode string w/ a bytes pattern or vice-versa → replacement string, pattern, and the search string must be of the same type
- Regular Expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used w/o invoking their special meaning
- this collides w/ Python's usage of the same character for the same purpose in string literals
- e.g., to match a literal backslash, one might have to write '\\\\' as the pattern string, b/c the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Pyuthon string literal.
- NOTE: any invalid escape sequences in Python's usage of the backslash in string literals now generate a SyntaxWarning and in the future this will become a SyntaxError
- this behaviour will happen even if it is a valid escape sequence for a regular expression
02 re 사용
1. re 함수
1) re.compile()
re.compile(pattern, flags=0)
-
- compile a regex pattern into a regex object, which can be used for matching using its match(), search() 등
- the expression's behaviour can be modified by specifying a flags value
- values can be any of the flags variables, combined using bitwise OR (the | operator)
- sequence:
prog = re.compile(pattern)
result = prog.match(string)
# above is equivalent to
result = re.match(pattern, string)
- using re.compile() and saving the resulting regex object for reuse is more efficient when the expression will be used several times in a single program
2) re.search()
re.search(pattern, string, flags=0)
- scan through string looking for the first location where the regex pattern produces a match, and return a corresponding Match
- return None if no position in the string matches the pattern
- NOTE: this is different from finding a zero-length match at some point in the string
참조
'Python > 기본문법' 카테고리의 다른 글
os (운영체제 작업 모듈) (1) | 2024.06.18 |
---|---|
sys (인터프리터 제어 모듈) (0) | 2024.06.13 |
파이썬 Type Hint (타입 정보 명시적 표시) (0) | 2024.05.08 |
Generator (제너레이터) (0) | 2024.05.03 |
dict.pop() 메소드 (0) | 2024.04.17 |