zoukankan      html  css  js  c++  java
  • boost regex expression

    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;
    }

    4. boost xpressive

    Like boost regex, boost xpressive provides functions to search strings using regular expressions. However, boost xpressive makes it possible to write down regular expressions as C++ code rather than strings. That makes it possible to check at compile time whether a regular expression is valid or not.

    #include <boost/xpressive/xpressive.hpp>
    #include <string>
    #include <iostream>
    
    using namespace boost::xpressive;
    
    int main() {
      std::string s = "Boost Libraries";
      sregex expr = sregex::compile("\w+\s\w+");
      std::cout << std::boolalpha << regex_match(s, expr) << std::endl;
      return 0;
    }

    boost::xpressive::regex_match() compares strings, boost::xpressive::regex_search() searches in strings, and boost::xpressive::regex_replace() replaces characters in strings. The type of regular expression in boost xpressive depends on the type of string being searched. Because s is based on std::string, the type of the regular expression must be boost::xpressive::sregex.

    #include <boost/xpressive/xpressive.hpp>
    #include <iostream>
    
    using namespace boost::xpressive;
    
    int main() {
       const char* c = "Boost Libraries";
       cregex expr = cregex::compile("\w+\s\w+");
       std::cout << std::boolalpha << regex_match(c, expr) << std::endl;
       return 0;
    }

    For strings of type const char*, use the class boost::xpressive::cregex.

  • 相关阅读:
    pdf 去水印 比较好用
    ffmpeg从视频中每隔几秒提取一张图片
    tampermonkey 网盘直链下载助手
    淘工厂买的usb 蓝牙5.0 外置台式机 适配器(下载)
    Apache Doris 轻松入门和快速实践
    一步一步编译最新版Apache Doris 0.15版本的详细过程
    go使用msgpack
    服务接口杂谈
    关于真伪3层所讨论的
    jattach最近的一些更新
  • 原文地址:https://www.cnblogs.com/sssblog/p/10981214.html
Copyright © 2011-2022 走看看