hio*_*obs 5 c++ regex boost boost-xpressive
说我有一个sregex像这样的对象:
boost::xpressive::sregex::compile("(?P<firstword>\\w+) (?<secondword>\\w+)!");
Run Code Online (Sandbox Code Playgroud)
我没有在xpressive文档中找到任何关于此的参考,尽管xpressive支持命名组就好了.
我知道可以通过组迭代很好,但是如何访问组名(如果组有名称)?
那么,我将如何遍历命名组?
假设我们拥有您正在处理的整个正则表达式,在我看来,您正在尝试创建一个与两个命名捕获都匹配的正则表达式,因此尝试迭代命名捕获是没有用的。
你只需要尝试类似的事情。
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;
}
Run Code Online (Sandbox Code Playgroud)
如果正则表达式是更复杂模式的一部分,为什么不使用静态命名捕获: 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
| 归档时间: |
|
| 查看次数: |
457 次 |
| 最近记录: |