Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Classes | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Algorithms | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Iterators | ||||
(C++11) | ||||
(C++11) | ||||
Exceptions | ||||
(C++11) | ||||
Traits | ||||
(C++11) | ||||
Constants | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Regex Grammar | ||||
(C++11) |
Member Functions | ||||
basic_regex::basic_regex | ||||
Observers | ||||
Locale | ||||
Modifiers | ||||
Constants | ||||
Non-member Functions | ||||
Deduction guides(C++17) |
basic_regex(); | (1) | (since C++11) |
explicit basic_regex(const CharT* s, flag_type f=std::regex_constants::ECMAScript); | (2) | (since C++11) |
basic_regex(const CharT* s,std::size_t count, flag_type f=std::regex_constants::ECMAScript); | (3) | (since C++11) |
basic_regex(const basic_regex& other); | (4) | (since C++11) |
basic_regex( basic_regex&& other)noexcept; | (5) | (since C++11) |
template<class ST,class SA> explicit basic_regex(conststd::basic_string<CharT,ST,SA>& str, | (6) | (since C++11) |
template<class ForwardIt> basic_regex( ForwardIt first, ForwardIt last, | (7) | (since C++11) |
basic_regex(std::initializer_list<CharT> init, flag_type f=std::regex_constants::ECMAScript); | (8) | (since C++11) |
Constructs a new regular expression from a sequence of characters interpreted according to the flagsf.
[
first,
last)
.s | - | pointer to a null-terminated string |
count | - | length of a character sequence used to initialize the regex |
first, last | - | range of a character sequence used to initialize the regex |
str | - | a basic_string used as a source used to initialize the regex |
other | - | another regex to use as source to initialize the regex |
init | - | initializer list used to initialize the regex |
f | - | flags used to guide the interpretation of the character sequence as a regular expression |
Type requirements | ||
-ForwardIt must meet the requirements ofLegacyForwardIterator. |
#include <iomanip>#include <iostream>#include <regex>#include <string> void match_and_print(conststd::string& text,conststd::regex& pattern){std::sregex_iterator it(text.begin(), text.end(), pattern), it_end;int count=0;for(; it!= it_end;++it){conststd::smatch& match=*it;std::cout<<++count<<". "<<std::quoted(match.str())<<'\n';}std::cout<<(count?"\n":"no match found\n\n");} int main(){conststd::string text="Hello, World! 12345"; // Matches one or more digitsstd::string pattern_text="\\d+";std::cout<<"digits ("<< pattern_text<<"):\n";auto pattern=std::regex(pattern_text); match_and_print(text, pattern); // Matches one or more characters split by space pattern_text="[^\\s]+";std::cout<<"words ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text); match_and_print(text, pattern); // Matches one or more characters split by space pattern_text="[a-zA-Z]+";std::cout<<"words without symbols and digits ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text); match_and_print(text, pattern); // Matches one non digits, non alphabet pattern_text="[^0-9A-Za-z]";std::cout<<"symbol ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text); match_and_print(text, pattern); // Matches one or more lowercase pattern_text="[a-z]+";std::cout<<"lowercase ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text); match_and_print(text, pattern); // Matches one or more lowercase with std::regex::icase flag pattern_text="[a-z]+";std::cout<<"lowercase with ignore case flag ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text, std::regex::icase); match_and_print(text, pattern); // Matches basic POSIX regular expression pattern_text="[[:digit:]]+";std::cout<<"basic POSIX regex ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text, std::regex::basic); match_and_print(text, pattern); // Matches extended POSIX regular expression pattern_text="[[:digit:]]+";std::cout<<"extended POSIX regex ("<< pattern_text<<"):\n"; pattern=std::regex(pattern_text, std::regex::extended); match_and_print(text, pattern);}
Output:
digits (\d+):1. "12345" words ([^\s]+):1. "Hello,"2. "World!"3. "12345" words without symbols and digits ([a-zA-Z]+):1. "Hello"2. "World" symbol ([^0-9A-Za-z]):1. ","2. " "3. "!"4. " " lowercase ([a-z]+):1. "ello"2. "orld" lowercase with ignore case flag ([a-z]+):1. "Hello"2. "World" basic POSIX regex ([[:digit:]]+):no match found extended POSIX regex ([[:digit:]]+):1. "12345"