使用boost::regex_search进行字符串提取
2011-05-09 21:38
1126 查看
使用正则表达式来能够处理很复杂的字符串,这里只分析以下如何使用boost::regex_search进行字符串提取。
主角登场:
m是匹配结果,是我们下面说的重点
e是正则表达式
flags是匹配规则
match_results结构的主要成员/方法:
bool empty() const : match_result是否为空,正常应为0(regex_search返回真时),regex_search返回假时match_results结构不确定
size_t size() const : 返回正则表达式中的元组数+1,元组即正则表达式的中的()
const_reference operator[](int n) const : 返回一个sub_match引用,当n=0时返回[first,last)的匹配结果,当0<n<=size时,返回对应元组的匹配结果,
当n>size时返回一个matched值为假的sub_match结构,元组按照左边括号的顺序和sub_match相对应,从1开始。
如regex:(([a-z]+).([0-9]+))+ m[1]:(([a-z]+).([0-9]+)) m[2]:([a-z]+) m[3]:([0-9]+)
sub_match结构的主要成员/方法:
bool matched: 是否匹配
什么是sub_match:
[/code]
Boost.Regex
转载请注明出处:
http://www.cnblogs.com/hucn/archive/2011/05/09/2041490.html
[/code]
主角登场:
// boost::regex_search 1 template <class BidirectionalIterator, class Allocator, class charT, class traits> 2 bool regex_search(BidirectionalIterator first, BidirectionalIterator last, 3 match_results<BidirectionalIterator, Allocator>& m, 4 const basic_regex<charT, traits>& e, 5 match_flag_type flags = match_default);
函数概要
判断在[first,last)内是否存在某个子序列匹配正则表达式e, 参数flags用来控制表达式如何匹配字符序列。 如果存在这样的序列则返回true,否则返回false。参数介绍
重载的函数很类似,这里我们只分析这份
first,last是指向我们要处理的串的首尾迭代器m是匹配结果,是我们下面说的重点
e是正则表达式
flags是匹配规则
match_results结构的主要成员/方法:
bool empty() const : match_result是否为空,正常应为0(regex_search返回真时),regex_search返回假时match_results结构不确定
size_t size() const : 返回正则表达式中的元组数+1,元组即正则表达式的中的()
const_reference operator[](int n) const : 返回一个sub_match引用,当n=0时返回[first,last)的匹配结果,当0<n<=size时,返回对应元组的匹配结果,
当n>size时返回一个matched值为假的sub_match结构,元组按照左边括号的顺序和sub_match相对应,从1开始。
如regex:(([a-z]+).([0-9]+))+ m[1]:(([a-z]+).([0-9]+)) m[2]:([a-z]+) m[3]:([0-9]+)
sub_match结构的主要成员/方法:
bool matched: 是否匹配
什么是sub_match:
typedef sub_match<const char*> csub_match; typedef sub_match<const wchar_t*> wcsub_match; typedef sub_match<std::string::const_iterator> ssub_match; typedef sub_match<std::wstring::const_iterator> wssub_match;
[/code]
例子
#include <string> #include <iostream> #include <boost\regex.hpp> void main() { std::string str = "192.168.1.1"; boost::regex expression("([0-9]+).([0-9]+).([0-9]+)"); boost::smatch what; if ( boost::regex_search(str, what, expression) ) { std::cout << what.size() << std::endl; for (size_t i = 0; i < what.size(); ++i) { if (what[i].matched) std::cout << what[i] << std::endl; } } }// Output:// 4// 192.168.1// 192// 168// 1
参考资料
Boost.Regex
转载请注明出处:
http://www.cnblogs.com/hucn/archive/2011/05/09/2041490.html
[/code]
相关文章推荐
- 使用boost::regex_search进行字符串提取
- 如何在使用了BOOST_REGEX_MATCH_EXTRA情况下提高boost::regex_search的执行速度
- 使用Boost Regex 的regex_search进行遍历搜索
- 使用Regex.Replace()进行字符串的替换
- 使用Boost Regex 的regex_search进行遍历搜索
- Swift - 使用NSDataDetector进行URL验证,及提取字符串中所有链接
- 字符串根据多个字符进行分割的一种方法(使用split而不是regex.split更不是replace)
- 有一串字符串a12hh123mm21m78 ,请将其中的数字提取出来存入数组,使用选择排序算法将其进行排序:
- 使用query ds进行搜索(search with query dsl)
- 使用Repeater绑定数据,及提取显示数据进行操作的一点小方法!(模板列)
- boost:regex分割字符串(带有'\'字符)
- 使用 Boost 的 IPC 和 MPI 库进行并发编程
- 使用regexp提取字符串
- sscanf进行字符串分离、提取。以及string与数值的相互转换
- c#使用输出html字符串进行导出excel功能时,常用到得几种格式:
- boost 库Regex使用
- Python使用re模块正则提取字符串中括号内的内容示例
- 正则表达式match和group的区别 具有相同模式的字符串使用组的提取案例及原理
- List的排序。在一个list里面放入"1","2"..."10","11"...,"99999","100000",使用字符串大小的顺序与解析成的数字的大小进行排序
- 本地化OR普通---Java中的字符串比较,按照使用习惯进行比较