The regular expressions library provides a class that represents regular expressions, which are a kind of mini-language used to perform pattern matching within strings. Almost all operations with regexes can be characterized by operating on several of the following objects:
Patterns and replacement strings support the following regular expression grammars:
Some grammar variations (such as case-insensitive matching) are also avaliable, see this page for details.
[edit] Main classesThese classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters.
regular expression objectThese functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.
attempts to match a regular expression to an entire character sequenceThe regex iterators are used to traverse the entire set of regular expression matches found within a sequence.
iterates through all regex matches within a character sequenceThis class defines the type of objects thrown as exceptions to report errors from the regular expressions library.
reports errors generated by the regular expressions libraryThe regex traits class is used to encapsulate the localizable aspects of a regex.
provides metainformation about a character type, required by the regex library#include <iostream> #include <iterator> #include <regex> #include <string> 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("(\\w+)"); 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 longer 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 20 words Words longer than 6 characters: 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].
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4