C++ regex 正则表达式的使用

在C++中,有三种正则表达式可以选择使用:C++11 regex、POSIX regex 和 boost regex
C++ regex函数有3个:regex_match、regex_search 、regex_replace

regex_match

regex_match是正则表达式匹配的函数,下面以例子说明

// regex_match example
#include <iostream>
#include <string>
#include <regex>
 
int main ()
{
 
  if (std::regex_match ("subject", std::regex("(sub)(.*)") ))
    std::cout << "string literal matched\n";
 
  std::string s ("subject");
  std::regex e ("(sub)(.*)");
  if (std::regex_match (s,e))
    std::cout << "string object matched\n";
 
  if ( std::regex_match ( s.begin(), s.end(), e ) )
    std::cout << "range matched\n";
 
  std::cmatch cm;    // same as std::match_results<const char*> cm;
  std::regex_match ("subject",cm,e);
  std::cout << "string literal with " << cm.size() << " matches\n";
 
  std::smatch sm;    // same as std::match_results<string::const_iterator> sm;
  std::regex_match (s,sm,e);
  std::cout << "string object with " << sm.size() << " matches\n";
 
  std::regex_match ( s.cbegin(), s.cend(), sm, e);
  std::cout << "range with " << sm.size() << " matches\n";
 
  // using explicit flags:
  std::regex_match ( "subject", cm, e, std::regex_constants::match_default );
 
  std::cout << "the matches were: ";
  for (unsigned i=0; i<sm.size(); ++i) {
    std::cout << "[" << sm[i] << "] ";
  }
 
  std::cout << std::endl;
 
  return 0;
}

输出如下:
在这里插入图片描述

regex_search

regex_match是另外一个正则表达式匹配的函数,下面是regex_search的例子。regex_search和regex_match的主要区别是:regex_match是全词匹配,而regex_search是搜索其中匹配的字符串

// regex_search example
#include <iostream>
#include <regex>
#include <string>
 
int main(){
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"
 
  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;
 
  while (std::regex_search (s,m,e)) {
    for (auto x=m.begin();x!=m.end();x++) 
      std::cout << x->str() << " ";
    std::cout << "--> ([^ ]*) match " << m.format("$2") <<std::endl;
    s = m.suffix().str();
  }
}

输出如下:
在这里插入图片描述

regex_replace

regex_replace是替换正则表达式匹配内容的函数,下面是regex_replace的例子


#include <regex> 
#include <iostream> 
 
int main() { 
    char buf[20]; 
    const char *first = "axayaz"; 
    const char *last = first + strlen(first); 
    std::regex rx("a"); 
    std::string fmt("A"); 
    std::regex_constants::match_flag_type fonly = 
        std::regex_constants::format_first_only; 
 
    *std::regex_replace(&buf[0], first, last, rx, fmt) = '\0'; 
    std::cout << &buf[0] << std::endl; 
 
    *std::regex_replace(&buf[0], first, last, rx, fmt, fonly) = '\0'; 
    std::cout << &buf[0] << std::endl; 
 
    std::string str("adaeaf"); 
    std::cout << std::regex_replace(str, rx, fmt) << std::endl; 
 
    std::cout << std::regex_replace(str, rx, fmt, fonly) << std::endl; 
 
    return 0; 
}

输出如下:

注意反斜杠字符(\)会被转义

std::regex e1 ("\\d");  //  \d -> 匹配数字字符
std::regex e2 ("\\\\"); //  \\ -> 匹配反斜杠字符

模式 “(a+)." 匹配 “aardvark” 将匹配到 aa,模式 "(a+?).” 匹配 “aardvark” 将匹配到 a

单个字符

[abc] 匹配 a, b 或 c.
[^xyz] 匹配任何非 x, y, z的字符

范围
[a-z] 匹配任何小写字母 (a, b, c, …, z).
[abc1-5] 匹配 a, b , c, 或 1 到 5 的数字.

猜你喜欢

转载自blog.csdn.net/gaoyz1/article/details/84888945
今日推荐