Greta 入门指南
2016-05-31 13:32
211 查看
GRETA是微软研究院推出的一个正则表达式模板类库,GRETA 包含的 C++ 对象和函数,使字符串的模式匹配和替换变得很容易,它们是:
rpattern: 搜索的模式
match_results/subst_results: 放置匹配、替换结果的容器
为了执行搜索和替换的操作,用户首先需要用一个描述匹配规则的字符串来显式初始化一个rpattern对象,然后把需要匹配的字符串作为参数,调用rpattern的函数,比如match()或者substitute(),就可以得到匹配后的结果。如果match()/substitute()调用失败,函数返回false,如果调用成功,函数返回true,此时,match_results对象存储了匹配结果。请看例子代码:
程序输出结果:
rpattern: 搜索的模式
match_results/subst_results: 放置匹配、替换结果的容器
为了执行搜索和替换的操作,用户首先需要用一个描述匹配规则的字符串来显式初始化一个rpattern对象,然后把需要匹配的字符串作为参数,调用rpattern的函数,比如match()或者substitute(),就可以得到匹配后的结果。如果match()/substitute()调用失败,函数返回false,如果调用成功,函数返回true,此时,match_results对象存储了匹配结果。请看例子代码:
#include <iostream> #include <string> #include "regexpr2.h" #ifdef _UNICODE #define tstring wstring #define tcout wcout #else #define tstring string #define tcout cout #endif using namespace std; using namespace regex; int main() { match_results results; tstring str( _T("The book cost $12.34") ); rpattern pat( _T("\\$(\\d+)(\\.(\\d\\d))?") ); // Match a dollar sign followed by one or more digits, // optionally followed by a period and two more digits. // The double-escapes are necessary to satisfy the compiler. match_results::backref_type br = pat.match( str, results ); if( br.matched ) { tcout << ("match success!") << endl; tcout << ("price: ") << br << endl; } else { tcout << ("match failed!") << endl; } return 0; }
程序输出结果:
match success! price: $12.34
相关文章推荐
- 第十三周项目3形状类族中的纯虚函数
- Linux如何查看进程、杀死进程、启动进程等常用命令
- 高德地图添加点击手势不响应
- scrollLoading插件实现图片延迟懒加载实例包涵html和js代码
- Sign in - CodeProject
- 第二阶段—个人工作总结09
- 第14周-项目2-两个成员的类模板(1)
- Linux下进程的内存相关信息的查看
- Kafka入门经典教程
- Android设计模式之代理模式(Proxy Pattern)
- 输入一棵二元树,从上往下按层打印每个节点,每层从左往右打印。利用队列。
- SetACL 使用方法详细参数中文解析
- Java基础回顾 : 对象序列化和反序列化
- LOG规范
- iOS Error = NSURLErrorDomain Code=-1022
- 据说看完这21个故事的人,30岁前都成了亿万富翁。你是下一个吗?
- jsp清空缓存
- 模糊查询防注入
- #MYSQL表的增删
- CydiaSubstrate的简单使用