C++11中也將正則表達式納入了新標準的一部分,不僅如此,它還支持了6種不同的正則表達式的語法,分別是:`ECMASCRIPT`、`basic`、`extended`、`awk`、`grep和egrep`。其中`ECMASCRIPT`是默認的語法,具體使用哪種語法我們可以在構造正則表達式的時候指定。
正則表達式庫提供表示正則表達式,這是一種用于字符串內執行模式匹配小型的語言的類。
主要的類:?
**basic_regex**?
regular expression object
**sub_match**?
identifies the sequence of characters matched by a sub-expression
**match_results**?
identifies one regular expression match, including all sub-expression matches
算法:?
These functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.
**regex_match**?
attempts to match a regular expression to an entire character sequence
**regex_search**?
attempts to match a regular expression to any part of a character sequence
**regex_replace**?
replaces occurrences of a regular expression with formatted replacement text
迭代器:?
The regex iterators are used to traverse the entire set of regular expression matches found within a sequence.
**regex_iterator**?
iterates through all regex matches within a character sequence
**regex_token_iterator**?
iterates through the specified sub-expressions within all regex matches in a given string or through unmatched substrings
異常:?
This class defines the type of objects thrown as exceptions to report errors from the regular expressions library.
**regex_error**?
reports errors generated by the regular expressions library
例子代碼:
~~~
#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 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 19 words
Words longer 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].
~~~
- 前言
- 吐血整理C++11新特性
- C++11新特性之std::function
- c++11特性之正則表達式
- c++11特性之Lambda表達式
- c++11特性之override和final關鍵字
- c++11特性之std::thread--初識
- c++11特性之std::thread--初識二
- c++11特性之initializer_list
- c++11特性之std::thread--進階
- c++11特性之std::thread--進階二
- C++11新特性之 CALLBACKS
- C++11新特性之 std::array container
- C++11新特性之 nullptr
- C++11新特性之 rvalue Reference(右值引用)
- C++11新特性之 Move semantics(移動語義)
- C++11新特性之 default and delete specifiers
- C++11新特性之 Static assertions 和constructor delegation
- 開始使用C++11的幾個理由
- C++11新特性之 std::future and std::async