정규식
- 파이썬에 내장된 작고 고도로 특수화된 프로그래밍 언어
- re 모듈을 통해 사용할 수 있음
- 어디서 사용하나요?
- 영어문장, 전자메일주소 등
- 이 문자열이 패턴과 일치합니까? 이 문자열의 어느 부분에 패턴과 일치하는 것이 있습니까?
정규식 함수
- 문자가 가진 원래 의미가 아닌 특별한 용도로 사용되는 문자
| Function | Description |
| findall() | Returns a list containing all matches 모든 일치 항목이 포함된 목록 반환 |
| search() | Returns a Match object if there is a match anywhere in the string 문자열 내 일치하는 객체가 있으면 Match object 반환 |
| split() | Returns a list where the string has been split at each match |
| sub() | Replaces one or many matches with a string 하나 이상의 일치 항목을 문자열로 변경 |
| match() | 문자열의 처음부터 정규식과 매치되는 조사 부합하지 않으면 None 반환 |
| finditer() | 정규식과 매치되는 모든 문자열을 반복 가능한 객체로 리턴 |
- match 객체의 메서드
- group: 매치된 문자열 리턴
- start: 매치된 문자열의 시작위치 리턴
- end: 매치된 문자열의 끝위치 리턴
- 매칭된 문자열의 마지막 문자 바로 다음 인덱스 반환
- span: 매치된 문자열의 시작,끝에 해당하는 튜플 리턴
- 매칭된 문자열의 마지막 문자 바로 다음 인덱스 반환
>>> m = p.match("python")
>>> m.group()
'python'
>>> m.start()
0
>>> m.end()
6
>>> m.span()
(0, 6)
메타 문자
- 문자가 가진 원래 의미가 아닌 특별한 용도로 사용되는 문자
| Character | Description | Example |
| [] | A set of characters [a-zA-Z]: 모든 알파벳 [0-9]: 모든 숫자 [^0-9]: 문자 클래스 안에 ^ 메타 문자는 not의 의미 = 숫자가 아닌 문자만 매치 |
"[a-m]" |
| \ | Signals a special sequence (can also be used to escape special characters) | "\d" |
| . | Any character (except newline character) | "he..o" ⚠️he[..]o: "he + .. + o" |
| ^ | Starts with | "^hello" |
| $ | Ends with | "planet$" |
| * | Zero or more occurrences | "he.*o" |
| + | One or more occurrences | "he.+o" |
| ? | Zero or one occurrences | "he.?o" txt = "hello"라면 ll 두 개이기 때문에 조건에 맞지 않음 => [ ] 반환 |
| {} | Exactly the specified number of occurrences 정확히 지정된 발생 횟수 |
"he.{2}o" "he.{2,5}o": "he + any character 2~5회 반복 + o" |
| | | Either or | "falls|stays" |
| () | Capture and group |
특수 시퀀스
| Character | Description | Example |
| \A | Returns a match if the specified characters are at the beginning of the string | "\AThe" |
| \b | Returns a match where the specified characters are at the beginning or at the end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\bain" r"ain\b" |
| \B | Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\Bain" r"ain\B" |
| \d | Returns a match where the string contains digits (numbers from 0-9) | "\d" |
| \D | Returns a match where the string DOES NOT contain digits | "\D" |
| \s | Returns a match where the string contains a white space character [ \t\n\r\f\v]: 일반 공백, 탭, 줄바꿈, 캐리지 리턴, 폼 피드, 수직 탭 |
"\s" |
| \S | Returns a match where the string DOES NOT contain a white space character | "\S" |
| \w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) [a-zA-Z0-9_] |
"\w" |
| \W | Returns a match where the string DOES NOT contain any word characters | "\W" |
| \Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
참고자료: https://wikidocs.net/4308
08-2 정규 표현식 시작하기
정규 표현식에서는 메타 문자(meta characters)를 사용한다. 먼저 메타 문자가 무엇인지 알아보자. [TOC] ## 정규 표현식의 기초, 메타 문자 메타 문자란 원…
wikidocs.net
https://docs.python.org/ko/3/howto/regex.html
Regular Expression HOWTO
Author, A.M. Kuchling < amk@amk.ca>,. Abstract: This document is an introductory tutorial to using regular expressions in Python with the re module. It provides a gentler introduction than the corr...
docs.python.org
https://www.w3schools.com/python/python_regex.asp
W3Schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
반응형
'Python' 카테고리의 다른 글
| [Python/level 0] 양꼬치 - 120830, O(n) vs O(1) 풀이법 비교 (0) | 2024.08.03 |
|---|---|
| [Python/level 0] ad 제거하기 - 181870, not in, pop과 remove를 사용하지 않은 이유 (1) | 2024.08.03 |
| [Python/level 0] 아이스 아메리카노 - 120819, [money // 5500, money % 5500] (0) | 2024.07.29 |
| [Python/level 0] 피자 나눠 먹기 (1) - 120814, (n-1) // 7+1 (2) | 2024.07.29 |
| [Python/Silver IV] 덱 - 10866, deque, split(" ") vs. split(), sys.stdin (0) | 2024.07.29 |