C++ 正则化知识
发布时间:2020-12-16 09:16:15 所属栏目:百科 来源:网络整理
导读:正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex或regexp。 正则表达式是一种文本模式。正则表达式是强大、便捷、高效的文本处理工具。正则表达式本身,加上如同一门袖珍编程语言的通用模式表示法(general patter
正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex或regexp。 正则表达式是一种文本模式。正则表达式是强大、便捷、高效的文本处理工具。正则表达式本身,加上如同一门袖珍编程语言的通用模式表示法(general pattern notation),赋予使用者描述和分析文本的能力。配合上特定工具提供的额外支持,正则表达式能够添加、删除、分离、叠加、插入和修整各种类型的文本和数据。 完整的正则表达式由两种字符构成:
一个正则表达式仅仅为一个字符串,它没有长度限制。 默认情况表达式中的字母区分大小写。 常用元字符见这篇博文。 在C++/C++11中,GCC版本是4.9.0及以上,VS版本为VS2013及以上时,会有regex头文件, 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 matchn",tmp.c_str()); else fprintf(stderr,can not matchn",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,can searchn",can not searchn",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: %sn",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 stringn"); 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(),"$2"); std::cout << result; // with flags: std::cout << std::regex_replace(s,"$1 and $2",std::regex_constants::format_no_copy); std::cout << std::endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |