正则表达式库
来自cppreference.com
< cpp
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
正则表达式库提供了表示正则表达式的类,它是一种用来执行字符串中的模式匹配的小型语言。
原文:
The regular expressions library provides a class that represents 正则表达式, which are a kind of mini-language used to perform pattern matching within strings.
正则表达式库还提供了为各种算法,迭代器,例外,和类型特征提供支持的工具类。
原文:
Also provided in the regular expressions library are utility classes that provide support for various algorithms, iterators, exceptions, and type traits.
目录 |
[编辑] 主要类
这些类封装了正则表达式,以及带有目标字符序列的正则表达式匹配结果。
原文:
These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters.
(C++11) |
正则表达式对象 (类模板) |
(C++11) |
标识子表达式匹配的字符序列 原文: identifies the sequence of characters matched by a sub-expression (类模板) |
(C++11) |
标识正则表达式的匹配结果,包含所有子表达式的匹配结果 原文: identifies one regular expression match, including all sub-expression matches (类模板) |
[编辑] 算法
这些函数可以将封装在regex中的正则表达式应用于目标字符序列。
原文:
These 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 the entire character sequence (函数模板) | |
(C++11) |
尝试以正则表达式匹配字符序列的任何部分 原文: attempts to match a regular expression to any part of the character sequence (函数模板) |
(C++11) |
以格式化的替换文本来替换正则表达式匹配的位置 (函数模板) |
[编辑] 迭代器
正则表达式用于遍历在序列中发现的正则表达式匹配结果的整个集合。
原文:
The regex iterators are used to traverse the entire set of regular expression matches found within a sequence.
(C++11) |
遍历正则表达式在字符序列中的所有匹配 原文: iterates through all regex matches within a character sequence (类模板) |
(C++11) |
在给定的字符串或者通过不匹配的子串,遍历所有匹配结果中的指定的子表达式 原文: 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.
(C++11) |
报告正则表达式库产生的错误 原文: reports errors generated by the regular expressions library (类) |
[编辑] 特征类
正则表达式特征类用于封装一个正则表达式的本地化特征。
原文:
The regex traits class is used to encapsulate the localizable aspects of a regex.
(C++11) |
提供字符类型的元信息,正则表达式库所需 原文: provides metainformation about a character type, required by the regex library (类模板) |
[编辑] 常量
定义于
std::regex_constants 名字空间 | |
(C++11) |
控制正则表达式行为的常用选项 原文: general options controlling regex behavior (typedef) |
(C++11) |
针对于匹配的选项 (typedef) |
(C++11) |
描述不同类型的匹配错误 原文: describes different types of matching errors (typedef) |
[编辑] 例子
#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].