如何在表达式中迭代命名组
How to iterate named groups in xpressive?
假设我有一个sregex
对象,像这样:
boost::xpressive::sregex::compile("(?P<firstword>\w+) (?<secondword>\w+)!");
我没能在表达式文档中找到任何关于这方面的参考,尽管表达式支持命名组很好。
我知道可以遍历组很好,但是我如何访问组名(如果组有名称的话)?
那么,我如何遍历命名组呢?
假设我们有你正在处理的整个正则表达式,在我看来,您似乎正在尝试创建一个匹配两个正则表达式的正则表达式命名捕获,因此尝试遍历命名捕获是无用的。
你只要试一下就可以了。
std::string str("foo bar");
sregex rx = sregex::compile("(?P<firstword>\w+) (?<secondword>\w+)!");
smatch what;
if(regex_search(str, what, rx))
{
std::cout << "char = " << what["firstword"] << what["secondword"] std::endl;
}
如果正则表达式是更复杂模式的一部分,为什么不使用静态命名的capture:http://www.boost.org/doc/libs/1_41_0/doc/html/xpressive/user_s_guide.html boost_xpressive.user_s_guide.named_captures.static_named_captures
相关文章:
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- C++如何使用lambda表达式来捕获上一次迭代的值
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- 射击时从列表中删除外星人 - 找出循环;"表达式:无法递增结束列表迭代器"
- 如何将正则表达式中组的每次迭代都作为一个单独的组
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 表达式:字符串迭代器不兼容,调用"SetDllDirectory"
- ranged-for 语句中的表达式是否在每次迭代时计算
- 表达式:映射/集合迭代器不可递增
- 从mpl-fold表达式中检索迭代器位置
- 有没有办法在一次迭代中进行任意数量的正则表达式替换
- 使用正则表达式替换 1 次迭代中的所有匹配项
- 基于范围的 for 循环是否缓存容器表达式,或每次迭代重新计算它
- 正则表达式迭代器在Cpp中不起作用
- 编译器是否会将这个表达式优化为一个临时常量,而不是每次迭代都解析它
- 迭代器不是可取消引用的堆栈(表达式树)和循环无法正常工作
- 如何在表达式中迭代命名组
- 调试断言失败!表达式列表迭代器不兼容
- 迭代:二进制表达式的操作数无效
- 如何迭代std::string中所有匹配的正则表达式,使其在c++11 std::regex中的起始位置