对C++ TR1 regular expressions的简单使用
来源:互联网 发布:阿里云ces oss 编辑:程序博客网 时间:2024/06/02 21:20
[1] regex_search
[2] regex_match
[3] Retrieve a Match tr1::cmatch
[4] regex_replace
[5] Case-insensitive std::tr1::regex_constants::icase
#include <iostream>
#include <string>
#include <regex>
#include <cassert>
using namespace std;
int main()
{
string str = "Hello world";
tr1::regex rx("ello");
// [1] regex_search
//assert( regex_search(str.begin(), str.end(), rx) );
if (regex_search(str.begin(), str.end(), rx))
{
cout<<"find it!"<<endl;// ok
}
else
{
cout<<"not find!"<<endl;
}
// [2] How Do I Do a Match? regex_match
str = "Hello world";
rx = "ello";
if (regex_match(str.begin(), str.end(), rx))
{
cout<<"find it!"<<endl;
}
else
{
cout<<"not find!"<<endl;// ok
}
// [3] How Do I Retrieve a Match?
tr1::cmatch res;
str = "<h2>Egg prices</h2>";
rx = "<h(.)>([^<]+)";
tr1::regex_search(str.c_str(), res, rx);
cout << res[1] << ". " << res[2] << "/n";// 2. Egg prices
// [4] How Do I Do a Replace? regex_replace
str = "Hello world";
rx = "world";
string replacement = "planet";
string str2 = std::tr1::regex_replace(str, rx, replacement);
cout<<str2<<endl;// Hello planet
// [5] How Do I Make a Regular Expression Case-insensitive?
str = "My name is wcdj";
tr1::regex rx2("WcdJ",std::tr1::regex_constants::icase);
//assert( regex_search(str.begin(), str.end(), rx) );
if (regex_search(str.begin(), str.end(), rx2))
{
cout<<"find it!"<<endl;// ok
}
else
{
cout<<"not find!"<<endl;
}
str = "My name is wcdj";
tr1::regex rx3("WcdJ");
//assert( regex_search(str.begin(), str.end(), rx) );
if (regex_search(str.begin(), str.end(), rx3))
{
cout<<"find it!"<<endl;
}
else
{
cout<<"not find!"<<endl;// ok
}
return 0;
}
参考:
[1] Quick Start for C++ TR1 Regular Expressions
http://www.codeproject.com/KB/string/TR1Regex.aspx
[2] Getting started with C++ TR1 regular expressions
http://www.johndcook.com/cpp_regex.html
转自:http://blog.csdn.net/delphiwcdj/article/details/6195446
- 对C++ TR1 regular expressions的简单使用
- 对C++ TR1 regular expressions的简单使用
- A TR1 Tutorial: Regular Expressions
- 使用C++TR1实现物流配送问题的简单模拟
- 一辈子受用的Regular Expressions
- Extended Regular Expressions的不同
- Regular Expressions
- Regular Expressions
- Regular Expressions
- Regular Expressions
- Regular Expressions
- regular-expressions
- C++:智能指针-TR1的shared_ptr和weak_ptr使用介绍
- C++:智能指针-TR1的shared_ptr和weak_ptr使用介绍
- std::tr1::bind的使用
- Regular Expressions (1) ---- What is Regular Expressions?
- Regular Expressions (1) ---- What is Regular Expressions?
- std::tr1::function, std::tr1::bind的使用
- 八款开源 Android 游戏引擎
- 链表的插入排序-不使用头结点
- django常用命令
- 一个AS老鸟的经验之谈
- __asm__ __volatile__内嵌汇编用法简述
- 对C++ TR1 regular expressions的简单使用
- rcp界面美化
- IP包的协议字段
- Python多线程编程方式2 threading库的介绍
- ZOJ 1382 A Simple Task
- makefile 学习
- SWT 绘图技术
- MPEG2-TS音视频同步原理
- json2 转字符串问题