zoukankan      html  css  js  c++  java
  • C++之正则表达式

    正则表达式是独立与任何语言本身的一个很大的话题。在C++中,regex就是涉及到正则表达式的内容。

    [a-z]+. xt:在这个正则表达式中,[a-z]标识匹配一个小写字母,+可以是前面的表达式匹配多次,因此[a-z]+能够匹配一个小写字母组成的字符串。

    在正则表达式中的一个 . 表示匹配任意字符,而 . 则表示匹配字符 . ,最后txt表示严格匹配txt三个字母。因此这个正则表达式索要匹配的内容就是由纯小写字母组成的文本文件。

    regex_match用于匹配字符串和正则表达式,有很多不同的重载形式。最简单一种形式就是传入string以及一个regex进行匹配,当匹配成功,会返回一个true,否则返回一个false。

    /*
    regex.cpp
    */
    #include<iostream>
    #include<string>
    #include<regex>
    using namespace std;
    
    int main()
    {
        string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
        std::regex txt_regex("[a-z]+\.txt");
        for (const auto &fname: fnames)
        {
            cout << fname << ": " << regex_match(fname, txt_regex) << endl;
        }
    
        return 0;
    }

    编译:

    g++ regex.cpp -o regex -std=c++11

    运行结果:

    foo.txt: 1

    bar.txt: 1

    test: 0

    a0.txt: 0

    AAA.txt: 0

    另一种常用的形式就是依次传入 std::string/std::smatch/std::regex 三个参数,其中 std::smatch 的本质其实是 std::match_results,在标准库中, std::smatch 被定义为了 std::match_results<std::string::const_iterator>,也就是一个子串迭代器类型的 match_results。使用 std::smatch 可以方便的对匹配的结果进行获取,例如:

    /*
    regex_1.cpp
    */
    #include <iostream>
    #include <string>
    #include <regex>
    
    int main() {
        std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
        std::regex base_regex("([a-z]+)\.txt");
        std::smatch base_match;
        for(const auto &fname: fnames) {
            if (std::regex_match(fname, base_match, base_regex)) {
                // sub_match 的第一个元素匹配整个字符串
                // sub_match 的第二个元素匹配了第一个括号表达式
                if (base_match.size() == 2) {
                    std::string base = base_match[1].str();
                    std::cout << "sub-match[0]: " << base_match[0].str() << std::endl;
                    std::cout << fname << " sub-match[1]: " << base << std::endl;
                }
            }
        }
    }

    运行结果:

    sub-match[0]: foo.txt

    foo.txt sub-match[1]: foo

    sub-match[0]: bar.txt

    bar.txt sub-match[1]: bar

  • 相关阅读:
    Vue组件之间传值
    Vue 调试工具
    组件注册
    组件化思想
    图书列表案例
    数组相关API
    Vue常用特性
    选项卡案例
    Vue模板语法
    案例选项卡
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/12239945.html
Copyright © 2011-2022 走看看