Regular expressions library
cppreference.com
< cpp
<regex> 헤더에 정의됨. |
정규표현식 라이브러리는 일종의 소형언어로 문자열내에서 패턴매칭을 수행하기 위한 정규표현식 클래스를 제공합니다.
또한 정규표현식 라이브러리 내부에는 각종 알고리즘, 반복자, 예외, 타입 특징을 지원하는 유틸리티 클래스들을 제공하고 있습니다.regex
Almost all operations with regexes can be characterized by operating on several of the following objects:
- 대상 시퀀스. 패턴을 찾고자 하는 문자열 시퀀스. 2개의 순환자로 정의되는 범위가 될 수도 있고, 널문자로 끝나는 문자열이나 std::string이 될 수 있다..
- 패턴. 정규표현식 그 자체이다. 일치된다라는 것이 무엇인지를 결정한다. std::basic_regex형의 객체로, 별도 구문을 가진 문자열로 이루어진다. 지원하는 문법 변종들에 대한 자세한 설명은 syntax_option_type을 참고.
- 일치 배열(Matched array). std::match_results형의 객체로, 일치되는 부분에 대한 정보를 얻을 수 있다.
- 문자열 바꾸기. 일치되는 부분을 어떻게 바꿀 것인지를 결정하는 문자열이다. 지원하는 문법 변종들에 대한 자세한 설명은 match_flag_type 참고
목차 |
[편집] 주요 클래스
이 클래스들은 정규표현식과 목표 문자열의 시퀀스에 대한 정규표현식의 매칭 결과가 캡슐화 되어 있습니다.
(C++11) |
regular expression object (class template) |
(C++11) |
identifies the sequence of characters matched by a sub-expression (class template) |
(C++11) |
identifies one regular expression match, including all sub-expression matches (class template) |
[편집] 알고리즘
이 함수는 regex로 캡슐화된 정규표현식을 목표 문자열 시퀀스에 적용하는데 사용합니다.
(C++11) |
attempts to match a regular expression to an entire character sequence (function template) |
(C++11) |
attempts to match a regular expression to any part of a character sequence (function template) |
(C++11) |
replaces occurrences of a regular expression with formatted replacement text (function template) |
[편집] 반복자
regex 반복자는 시퀀스에서 발견한 정규표현식의 매칭의 모든 집합을 순회하는데 사용합니다.
(C++11) |
iterates through all regex matches within a character sequence (class template) |
(C++11) |
iterates through the specified sub-expressions within all regex matches in a given string or through unmatched substrings (class template) |
[편집] 예외
정규 표현식 라이브러리에서 에러를 리포팅 하기위해 예외를 던지는데 사용하는 객체의 타입 정의하는 클래스 입니다.
(C++11) |
reports errors generated by the regular expressions library (class) |
[편집] 특징
regex 특징 클래스는 regex의 지역화에 대해 캡슐화 하는데 사용합니다.
(C++11) |
provides metainformation about a character type, required by the regex library (class template) |
[편집] Constants
Defined in namespace
std::regex_constants | |
(C++11) |
general options controlling regex behavior (typedef) |
(C++11) |
options specific to matching (typedef) |
(C++11) |
describes different types of matching errors (typedef) |
[편집] Example
코드 실행
#include <iostream> #include <iterator> #include <string> #include <regex> int main() { std::string s = "Some people, when confronted with a problem, think " "\"I know, I'll use regular expressions.\" " "Now they have two problems."; std::regex self_regex("REGULAR EXPRESSIONS", std::regex_constants::ECMAScript | std::regex_constants::icase); if (std::regex_search(s, self_regex)) { std::cout << "Text contains the phrase 'regular expressions'\n"; } std::regex word_regex("(\\S+)"); auto words_begin = std::sregex_iterator(s.begin(), s.end(), word_regex); auto words_end = std::sregex_iterator(); std::cout << "Found " << std::distance(words_begin, words_end) << " words\n"; const int N = 6; std::cout << "Words greater than " << N << " characters:\n"; for (std::sregex_iterator i = words_begin; i != words_end; ++i) { std::smatch match = *i; std::string match_str = match.str(); if (match_str.size() > N) { std::cout << " " << match_str << '\n'; } } std::regex long_word_regex("(\\w{7,})"); std::string new_s = std::regex_replace(s, long_word_regex, "[$&]"); std::cout << new_s << '\n'; }
Output:
Text contains the phrase 'regular expressions' Found 19 words Words greater than 6 characters: people, confronted problem, regular expressions." problems. Some people, when [confronted] with a [problem], think "I know, I'll use [regular] [expressions]." Now they have two [problems].