zoukankan      html  css  js  c++  java
  • C++ 正则化知识

    正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex或regexp。

    正则表达式是一种文本模式。正则表达式是强大、便捷、高效的文本处理工具。正则表达式本身,加上如同一门袖珍编程语言的通用模式表示法(general pattern notation),赋予使用者描述和分析文本的能力。配合上特定工具提供的额外支持,正则表达式能够添加、删除、分离、叠加、插入和修整各种类型的文本和数据。

    完整的正则表达式由两种字符构成:

    1. 特殊字符(special characters)称为”元字符”(meta characters),
    2. 其它为”文字”(literal),或者是普通文本字符(normal text characters,如字母、数字、汉字、下划线)。
      正则表达式的元字符提供了更强大的描述能力。

    一个正则表达式仅仅为一个字符串,它没有长度限制。
    “子表达式”指的是整个正则表达式中的一部分,通常是括号内的表达式,或者是由”|”分割的多选分支。

    默认情况表达式中的字母区分大小写。

    常用元字符见这篇博文

    在C++/C++11中,GCC版本是4.9.0及以上,VS版本为VS2013及以上时,会有regex头文件,
    此头文件中会有regex_match、regex_search、regex_replace等函数可供调用,以下是测试代码:

    regex_match示例

    #include "regex.hpp"
    #include <regex>
    #include <string>
    #include <vector>
    #include <iostream>
    
    int test_regex_match()
    {
        std::string pattern{ "\d{3}-\d{8}|\d{4}-\d{7}" }; // fixed telephone
        std::regex re(pattern);
    
        std::vector<std::string> str{ "010-12345678", "0319-9876543", "021-123456789"};
    
        /* std::regex_match:
            判断一个正则表达式(参数re)是否匹配整个字符序列str,它主要用于验证文本
            注意,这个正则表达式必须匹配被分析串的全部,否则返回false;如果整个序列被成功匹配,返回true
        */
    
        for (auto tmp : str) {
            bool ret = std::regex_match(tmp, re);
            if (ret) fprintf(stderr, "%s, can match
    ", tmp.c_str());
            else fprintf(stderr, "%s, can not match
    ", tmp.c_str());
        }
    
        return 0;
    }
    

    regex_search示例

    int test_regex_search()
    {
        std::string pattern{ "http|hppts://\w*$" }; // url
        std::regex re(pattern);
    
        std::vector<std::string> str{ "http://blog.csdn.net/fengbingchun", "https://github.com/fengbingchun",
            "abcd://124.456", "abcd https://github.com/fengbingchun 123" };
    
        /* std::regex_search:
            类似于regex_match,但它不要求整个字符序列完全匹配
            可以用regex_search来查找输入中的一个子序列,该子序列匹配正则表达式re
        */
    
        for (auto tmp : str) {
            bool ret = std::regex_search(tmp, re);
            if (ret) fprintf(stderr, "%s, can search
    ", tmp.c_str());
            else fprintf(stderr, "%s, can not search
    ", tmp.c_str());
        }
    
        return 0;
    }
    
    int test_regex_search2()
    {
        std::string pattern{ "[a-zA-z]+://[^\s]*" }; // url
        std::regex re(pattern);
    
        std::string str{ "my csdn blog addr is: http://blog.csdn.net/fengbingchun , my github addr is: https://github.com/fengbingchun " };
        std::smatch results;
        while (std::regex_search(str, results, re)) {
            for (auto x : results)
                std::cout << x << " ";
            std::cout << std::endl;
            str = results.suffix().str();
        }
    
        return 0;
    }
    

    regex_replace示例

    int test_regex_replace()
    {
        std::string pattern{ "\d{18}|\d{17}X" }; // id card
        std::regex re(pattern);
    
        std::vector<std::string> str{ "123456789012345678", "abcd123456789012345678efgh",
            "abcdefbg", "12345678901234567X" };
        std::string fmt{ "********" };
    
        /* std::regex_replace:
            在整个字符序列中查找正则表达式re的所有匹配
            这个算法每次成功匹配后,就根据参数fmt对匹配字符串进行替换
        */
    
        for (auto tmp : str) {
            std::string ret = std::regex_replace(tmp, re, fmt);
            fprintf(stderr, "src: %s, dst: %s
    ", tmp.c_str(), ret.c_str());
        }
    
        return 0;
    }
    
    int test_regex_replace2()
    {
        // reference: http://www.cplusplus.com/reference/regex/regex_replace/
        std::string s("there is a subsequence in the string
    ");
        std::regex e("\b(sub)([^ ]*)");   // matches words beginning by "sub"
    
        // using string/c-string (3) version:
        std::cout << std::regex_replace(s, e, "sub-$2");
    
        // using range/c-string (6) version:
        std::string result;
        std::regex_replace(std::back_inserter(result), s.begin(), s.end(), e, "$2");
        std::cout << result;
    
        // with flags:
        std::cout << std::regex_replace(s, e, "$1 and $2", std::regex_constants::format_no_copy);
        std::cout << std::endl;
    
        return 0;
    }
    
    
  • 相关阅读:
    反向迭代
    c++知识点
    LeetCode-Count Bits
    LeetCode-Perfect Rectangle
    LeetCode-Perfect Squares
    LeetCode-Lexicographical Numbers
    LeetCode-Find Median from Data Stream
    LeetCode-Maximal Square
    LeetCode-Number of Digit One
    LeetCode-Combination Sum IV
  • 原文地址:https://www.cnblogs.com/lvjincheng/p/11509548.html
Copyright © 2011-2022 走看看