正则表达式库

定义于头文件 <regex>

正则表达式库提供表示正则表达式的类,正则表达式是一种用于在字符串中匹配模式的微型语言。下列数种对象上的操作能刻画几乎所有带正则表达式的操作:

目录

主类

这些类封装正则表达式和在字符的目标序列中匹配正则表达式的结果。

(C++11)
正则表达式对象
(类模板)
(C++11)
标识子表达式所匹配的字符序列
(类模板)
标识一个正则表达式匹配,包含所有子表达式匹配
(类模板)

算法

这些算法将封装于 regex 的正则表达式应用到字符的目标序列。

(C++11)
试图匹配正则表达式到整个字符序列
(函数模板)
(C++11)
试图匹配正则表达式到字符序列的任何部分
(函数模板)
以格式化的替换文本来替换正则表达式匹配的出现位置
(函数模板)

迭代器

regex_iterator 用于遍历在序列中找到的匹配正则表达式的整个集合。

在字符序列中通过所有正则表达式匹配迭代
(类模板)
通过在给定的字符串中所有正则表达式匹配中的指定子表达式,或通过不匹配的子串迭代
(类模板)

异常

此类定义作为异常抛出以报告来自正则表达式库错误的类型。

(C++11)
报告正则表达式库生成的错误
(类)

特性

regex_traits 类用于封装 regex 的本地化方面。

(C++11)
提供正则表达式库所需的关于字符类型的元信息
(类模板)

常量

定义于命名空间 std::regex_constants
控制正则表达式行为的通用选项
(typedef)
特定到匹配的选项
(typedef)
(C++11)
描述匹配错误的不同类型
(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].

版本历史

  • (当前 | 先前 2017年6月16日 (五) 05:51Fruderica讨论 | 贡献. . (3,941字节) (-429). . (撤销)
  • 当前 | 先前 2016年2月4日 (四) 17:06Wizardforcel讨论 | 贡献. . (4,370字节) (+1,621). . (add example) (撤销)
  • 当前 | 先前 2016年2月4日 (四) 17:06Wizardforcel讨论 | 贡献. . (2,749字节) (-2). . (撤销)
  • 当前 | 先前 2016年2月4日 (四) 17:01Wizardforcel讨论 | 贡献. . (2,751字节) (-4). . (撤销)
  • 当前 | 先前 2013年7月9日 (二) 02:09P12bot讨论 | 贡献 . . (2,755字节) (+6). . (Allow search engines to index popular pages.) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:29P12bot讨论 | 贡献 . . (2,749字节) (-260). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 12:53P12bot讨论 | 贡献 . . (3,009字节) (+154). . (r2.7.3) (机器人添加:ar, de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 09:05P12讨论 | 贡献 . . (2,855字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (2,855字节) (-70). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 15:32P12讨论 | 贡献 . . (2,925字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (2,925字节) (+2,925). . (Translated from the English version using Google Translate)