Regular expressions library
正则表达式库
在标头中定义<regex>
*。
正则表达式库提供了一个表示正则表达式,这是一种用于在字符串中执行模式匹配的微型语言。几乎所有使用regexes的操作都可以通过对以下几个对象的操作来实现:
目标序列
搜索模式的字符序列。这可能是由两个迭代器指定的范围,以空结尾的字符串或std::string
...
花纹
这是正则表达式本身。它决定什么构成匹配。它是一个类型的对象。std::basic_regex
,由具有特殊语法的字符串构造。见句法[医]期权[医]类型用于描述支持的语法变体。
匹配阵列
有关匹配的信息可以作为类型对象检索。std::match_results
...
替换串
这是一个确定如何替换匹配的字符串,请参见匹配[医]旗子[医]类型用于描述支持的语法变体。
主要课程
这些类封装了正则表达式以及在目标字符序列中匹配正则表达式的结果。
basic_regex (C++11) | regular expression object (class template) |
---|---|
sub_match (C++11) | identifies the sequence of characters matched by a sub-expression (class template) |
match_results (C++11) | identifies one regular expression match, including all sub-expression matches (class template) |
算法
这些函数用于将封装在正则表达式中的正则表达式应用于目标字符序列。
regex_match (C++11) | attempts to match a regular expression to an entire character sequence (function template) |
---|---|
regex_search (C++11) | attempts to match a regular expression to any part of a character sequence (function template) |
regex_replace (C++11) | replaces occurrences of a regular expression with formatted replacement text (function template) |
迭代器
regex迭代器用于遍历序列中找到的整组正则表达式匹配。
regex_iterator (C++11) | iterates through all regex matches within a character sequence (class template) |
---|---|
regex_token_iterator (C++11) | iterates through the specified sub-expressions within all regex matches in a given string or through unmatched substrings (class template) |
例外
该类将抛出的对象类型定义为从正则表达式库报告错误的异常。
regex_error (C++11) | reports errors generated by the regular expressions library (class) |
---|
性状
regex特性类用于封装regex的可本地化方面。
regex_traits (C++11) | provides metainformation about a character type, required by the regex library (class template) |
---|
常数
在命名空间std::regex中定义[医]常数
*。
句法[医]期权[医]类型%28C++11%29控制正则表达式行为的一般选项%28
匹配[医]旗子[医]类型%28C++11%29选项特定于匹配%28
误差[医]类型%28C++11%29描述不同类型的匹配错误%28
例
二次
#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';
}
二次
产出:
二次
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].
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。