regex C++正则表达式简单使用
来源:互联网 发布:淘宝卖东西提成怎么算 编辑:程序博客网 时间:2024/05/22 06:14
/* * 正则表达式是在运行时而非编译时编译的 * 构造一个regex或对一个已有的regex赋新值可能非常耗时 * 我们应该尽量避免不必要的regex */#include <iostream>#include <regex>#include <string>using namespace std;int main2(){ //搜索关键字为ei且前面不是紧接c string pattern("[^c]ei"); pattern = "[[:alpha:]]*"+pattern+"[[:alpha:]]*"; regex r(pattern); //smatch 容器类,保存string中搜索的结果 smatch result; string test_str = "recepit freind theif receive";// if(regex_search(test_str,result,r)){ //regex_search找到一个就停止了// cout<<result.str()<<endl;// } int i=0; while (std::regex_search (test_str,result,r)) { for (auto x:result) std::cout << i++ << " :" << x << " "<<std::endl; test_str = result.suffix().str();//当前结果的后续字段 test_str.erase(0,test_str.find_first_not_of(" ")); cout<<test_str<<endl; } return 0;}int main1 (){ std::string s ("there is a needle in this haystack"); std::smatch m; std::regex e ("needle"); std::cout << "searching for needle in [" << s << "]\n"; std::regex_search ( s, m, e ); if (m.ready()) { std::cout << m[0] << " found!\n"; std::cout << "prefix: [" << m.prefix() << "]\n"; std::cout << "suffix: [" << m.suffix() << "]\n"; } return 0;}int main(){ std::string s ("there is a needle in this haystack"); //match参数类型与输入序列不匹配,在下面的regex_search使用了一个char* std::cmatch m; std::regex e ("needle"); std::cout << "searching for needle in [" << s << "]\n"; std::regex_search ( "there is a needle in this haystack", m, e ); cout<<m[0]<<endl; return 0;}调整main的结构(去掉main后面的数字把其他的加上数字让它只有一个main有效)调出的结果依次是:
阅读全文
0 0
- regex C++正则表达式简单使用
- C++:Regex正则表达式
- C++:Regex正则表达式
- C++:Regex正则表达式
- 简单的正则表达式 Regex
- 【C/C++】boost regex正则表达式的安装及使用
- C语言正则表达式regex
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
- 使用开源库 Objective-C RegEx Categories 处理正则表达式
- c/c++ regex正则使用
- c语言中的正则表达式regex.h
- c语言中的正则表达式regex.h
- Linux C 正则表达式运用(regex.h)
- 日志 c/c++ 正则表达式 regex
- C语言的正则表达式 regex
- C#Regex正则表达式学习笔记
- C语言正则表达式库RegEx库
- 个人理解总结之抽象工厂模式
- git ——回退过去、回到未来
- 第5章 遍历二叉树【递归遍历】
- js 验证码 倒计时60秒
- SQL Server 2017 安装AdventureWorks示例数据库出现的问题
- regex C++正则表达式简单使用
- UVA 12626 I ❤ Pizza
- action里获取表单数据的三种方式
- 安装nodejs和npm以后出现 /usr/bin/env: node: No such file or directory
- 1045. 快速排序(25)
- spring MVC视图解析器
- 色彩表示与编码
- express源码(1)
- JAVA-io流