码迷,mamicode.com
首页 > 其他好文 > 详细

boost regex expression

时间:2019-06-05 20:07:56      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:style   cout   ssi   ace   let   VID   exp   out   reg   

Boost.Regex provides three different functions to search for regular expressions

1. regex_match

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main() {
  std::string s = "Boost Libraries";
  boost::regex expr("\\w+\\s\\w+");
  std::cout << std::boolalpha << boost::regex_match(s, expr) << std::endl;
  return 0;
}

boost::regex_match() compares a string with a regular expression. It will return true only if the expression matches the complete string.

2. regex_search

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main() {
  std::string s = "Boost Libraries";
  boost::regex expr("(\\w+)\\s(\\w+)");
  boost::smatch what;
  if (boost::regex_search(s, what, expr)) {
    std::cout << what[0] << std::endl;
    std::cout << what[1] << "_" << what[2] << std::endl;
  }
  return 0;
}

3. regex_replace

#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main() {
  std::string s = " Boost Libraries";
  boost::regex expr("\\s");
  std::string fmt("_");
  std::cout << boost::regex_replace(s, expr, fmt) << std::endl;
  return 0;
}

 

boost regex expression

标签:style   cout   ssi   ace   let   VID   exp   out   reg   

原文地址:https://www.cnblogs.com/sssblog/p/10981214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!