如何使用新的c++0x正则表达式对象在字符串中重复匹配
How do I use the new c++0x regex object to match repeatedly within a string?
我有一个字符串:
"hello 1, hello 2, hello 17, and done!"
我想反复应用这个正则表达式:
hello ([0-9]+)
并且能够以某种方式迭代匹配及其捕获组。我已经在c++0x中成功地使用了"regex"内容来查找字符串中某个内容的第一个匹配项,并检查捕获组的内容;然而,在找到所有匹配项之前,我不知道如何在字符串上多次执行此操作。帮助
(如果重要的话,平台是visual studio 2010。(
不要使用regex_match,使用regex_search。您可以在此处找到示例:http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339.
这应该可以做到(注意,我直接在浏览器中输入,没有编译它(:
#include <iostream>
#include <regex>
int main()
{
// regular expression
const std::regex pattern("hello ([0-9]+)");
// the source text
std::string text = "hello 1, hello 2, hello 17, and done!";
const std::sregex_token_iterator end;
for (std::sregex_token_iterator i(text.cbegin(), text.cend(), pattern);
i != end;
++i)
{
std::cout << *i << std::endl;
}
return 0;
}
相关文章:
- 如何删除作为C++联合的成员变量的字符串对象
- 系统C 从字符串对象sc_uint
- 为什么<string>字符串对象需要 #include 而不是字符串文本?
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- C 字符串对象错误字符
- 创建兼容的字符串对象
- 从对象数组打印字符串对象
- 将指针分配给字符串对象的第一个也是最后一个索引
- 比较字符串对象等于向量中的元素
- 传递给 std::runtime_error 的 ctor 的字符串对象的生命周期是多少?
- 如何移动 std::ostringstream 的底层字符串对象?
- 将 const 引用传递给临时字符串对象在 C++ 中安全吗?
- getline 没有为字符串对象分配任何值
- 在c++中删除字符串对象
- 如何在没有收到需要分号编译错误的情况下从 c++ 中的函数返回字符串对象
- 你能在C++中返回字符串对象的局部变量吗?
- 将字符串对象的地址传递给函数
- C++:从字符串对象中提取整数
- 是否有理由使用字符串 => 索引到向量的映射,而不是字符串 => 对象?
- 控制对多线程程序中字符串对象的访问的最佳方法