c++ 正则表达式-基础操作

regex_match(s, re)    目标字符串s和正则表达式re是否完全匹配
regex_search(s, match_result, re)    目标字符串s是否存在某个子串与正则表达式re匹配
regex_replace(s, re, s1)    用s1替换目标字符串s中与正则表达式re匹配的子串
#include <iostream>
#include <string>
#include <regex>
using namespace std;

void regex_match_test()
{
    /* std::regex_match:
        判断一个正则表达式(参数re)是否匹配整个字符序列str,它主要用于验证文本
        注意:  这个正则表达式必须匹配被分析串的全部,
               否则返回false;如果整个序列被成功匹配,返回true
    */
    string s = "ccaatt";
    vector<regex> regs;
    vector<string> patterns = {
        "ccaatt",
        "cca.tt",            // .    匹配除换行符以外的任意字符。
        "cca[a,b,c,d,e]tt",  // []   字符类,匹配方括号中包含的任意字符。
        "cc[^b,c,d,e]att",   // [^]  否定字符类。匹配方括号中不包含的任意字符
        "ccaa*tt",           // *    匹配前面的子表达式零次或多次
        "c+a+t+",            // +    匹配前面的子表达式一次或多次
        "cc?aatt?",          // ?    匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。 
                             //      此处说明一下,出现一次或零次的意思可以理解为?前面的字符可出出现可不出现
                             //      注意它与 * 的区别 
        "ccaat{1,2}",        //{n,m} 花括号,匹配前面字符至少 n 次,但是不超过 m 次。 
        "(ccaatt)",          //(xyz)    字符组,按照确切的顺序匹配字符xyz。
        "(c(h|d|f)aatt)|(ccaatt)",    // |   分支结构,匹配符号之前的字符或后面的字符。
                                      // \   转义符,它可以还原元字符原来的含义,允许你匹配保留字符 [ ] ( ) { } . * + ? ^ $ \ |
        "^ccaatt",                        // ^   匹配行的开始
        "ccaatt$"                      // $   匹配行的结束
    };

    for (int i = 0; i < patterns.size(); i++)
        regs.push_back(regex(patterns[i]));

    for (int i = 0; i < regs.size(); i++)
    {
        if (regex_match(s, regs[i]))
            printf("use regex: %25s to match %10s [Match!]\n", patterns[i].c_str(), s.c_str());
        else
            printf("use regex: %25s to match %10s [Failed!]\n", patterns[i].c_str(), s.c_str());
    }
}

void regex_search_test()
{
    /* std::regex_search:
    类似于regex_match,但它不要求整个字符序列完全匹配
    可以用regex_search来查找输入中的一个子序列,
    注意regex_search匹配成功一个后面的不再匹配
    */

    string lines[] = { "Roses are #ff0000",
                      "violets are #0000ff",
                      "all of my base are belong to you" };

    regex color_regex("#[a-f0-9]{2}""[a-f0-9]{2}""[a-f0-9]{2}");

    // 简单匹配
    for (const auto &line : lines) {
        std::cout << line << ": " << std::boolalpha
            << std::regex_search(line, color_regex) << '\n';
    }
    std::cout << '\n';

    // 展示每个匹配中有标记子表达式的内容
    smatch color_match;
    for (const auto& line : lines) {
        if (regex_search(line, color_match, color_regex)) {
            cout << "matches for '" << line << "'\n";
            cout << "Prefix: '" << color_match.prefix() << "'\n";
            cout << color_match[0] << '\n';
            cout << "Suffix: '" << color_match.suffix() << "\'\n\n";
        }
    }

    // 重复搜索(参阅 std::regex_iterator )
    std::string log(R"(   \
        Speed:    366       \
        Mass:    35        \
        Speed:    378       \
        Mass:    32        \
        Speed:    400       \
        Mass:    30)");
    regex r(R"(Speed:\t\d*)");
    smatch sm;
    while (regex_search(log, sm, r))
    {
        std::cout << sm.str() << '\n';
        log = sm.suffix();
    }

    // C 风格字符串演示
    cmatch cm;
    if (regex_search("this is a test", cm, regex("test")))
        cout << "\nFound " << cm[0] << " at position " << cm.prefix().length() << endl;
}

void regex_replace_test()
{
    std::string text = "{{ name }} for brown fox", replace_result;
    std::regex double_brace("(\\{\\{) (.*) (\\}\\})");

    //regex_replace返回值即为替换后的字符串 
    replace_result = regex_replace(text, double_brace, "*");
    cout << replace_result << "\n";

    //构造存有结果的字符串,[$&]即为用[]将匹配成功部分括起来 
    // $&    Inserts the matched substring.     
    cout << regex_replace(text, double_brace, "[$&]") << '\n';

    // $i则输出double_brace中第i个括号匹配到的值 
    cout << regex_replace(text, double_brace, "$1") << '\n';
    cout << regex_replace(text, double_brace, "[$2]") << '\n';
    cout << regex_replace(text, double_brace, "$3") << '\n';
}

int main()
{
    /*regex_match(s, re)    目标字符串s和正则表达式re是否完全匹配
    regex_search(s, match_result, re)    目标字符串s是否存在某个子串与正则表达式re匹配
    regex_replace(s, re, s1)    用s1替换目标字符串s中与正则表达式re匹配的子串*/
        
    cout << "regex_match examples : " << endl;
    regex_match_test();

    cout << endl << "regex_search examples : " << endl;
    regex_search_test();

    cout << endl << "regex_replace examples : " << endl << endl;
    regex_replace_test();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++老师机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值