zoukankan      html  css  js  c++  java
  • boost之regex使用范例(wstring 处理经典)

    #include <regex>
    #include <list> 

     对wchar_t*字符串的search调用方法:

    boost::wcmatch match;

    boost::wregex reg(L"\\d{3}你");
    boost::regex_search(L"345你好啊!", match, reg);
    bool mt0 = match[0].matched;
    bool mt1 = match[1].matched;
    bool mt2 = match[2].matched;
    int ln = match.length();

     对std::wstring调用search方法

    boost::wsmatch match;
    std::wstring str(L"1、运行IVM解器安装程序\
    ivmdecoder1.7.1.cn.exe\
    2、直接使用鼠标左键双击ivm视频文件即可观看");
    boost::wregex reg(L"\\w+(\\d\\.\\d\\.\\d)(\\.\\w+\\.\\w+)");
    boost::regex_search(str, match, reg);
    int match_size = match.size();
    bool mt0 = match[0].matched;
    bool mt1 = match[1].matched;
    bool mt2 = match[2].matched;
    bool mt3 = match[3].matched;
    int ln = match.length();
    std::wstring s1 = match[0];
    std::wstring s2 = match[1];
    std::wstring s3 = match[2];

    std::wstring s4 = match[3]; 

     因为表达式里有两个分组,所以加上整个字符串共有3个match

     最后一个也就是match[3]是没有值的

     对std::wstring调用match方法:

    boost::wsmatch match;
    boost::wregex reg(L"[^\\d]+(\\d{4})[^\\d]+");
    std::wstring ws(L"大家4578好,哈哈!");
    bool match_status = boost::regex_match(ws, match, reg);
    if (match_status) {
     

    对std::wstring 调用replace方法

    boost::wregex reg(L"\\d{3}你",
    boost::wregex::icase|boost::wregex::perl);
    std::wstring str(L"345你好啊!");

     str=boost::regex_replace(str, reg, L"不"); 

    对std::wstring调用split方法,得到多个std::wstring

    std::wstring ws1;
    std::list<std::wstring> list;
    boost::wregex e(L";|\\|",
                 boost::regex::normal | boost::regbase::icase);
    std::wstring str = L"你好;他好;我好;哈哈|来吧";
    boost::regex_split(std::back_inserter(list), str, e);
     
    std::wstring s;
    while(list.size())
    {
    s = *(list.begin());
    list.pop_front();
    //cout << s << endl;

  • 相关阅读:
    sql性能查询
    ASP.Net Web应用程序与EXCEL交互时遇到的权限问题
    Connection strings for Excel 2007
    获取异常的具体出处dbms_utility.format_error_backtrace
    C#获取Excel架构信息的方法
    Oracle强杀进程
    C#游标溢出(访问数据库)解决方案。
    Visual C# 2008 调试技巧一
    【POI】修改Excel内容
    【Eclipse】在Eclipse工具中自定义类注释
  • 原文地址:https://www.cnblogs.com/carl2380/p/2307562.html
Copyright © 2011-2022 走看看