C++ Subpattern Matching

后端 未结 3 643
遥遥无期
遥遥无期 2021-01-15 01:51

can anyone please show me an example about using regex (regex.h) in C/C++ to search and/or extracting subpattern in a regex.

in javascript, it will be something like

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 02:12

    The only regular expressions readily available in C++ is boost::regex, and that is what has been adopted for the next standard. And the syntax is:

    boost::regex expr( "the string contains (\\d*) dots and (\\d*) chars" );
    boost::smatch match;
    if ( regex_match( text, match, expr ) ) {
        //  We have a match,
        std::string dots = match[1];
        std::string chars = match[2];
        //  ...
    }
    

提交回复
热议问题