[英]Get string between 2 delimiters using C++ STl TR1 regular expressions
I want to extract string between open and close bracket.So if the content is 我想在开括号和闭括号之间提取字符串。所以如果内容是
145(5)(7) 145(5)(7)
the output must be 输出必须是
5 5
7 7
I tried with C++ STL TR1 using below code 我尝试使用下面的代码与C ++ STL TR1
const std::tr1::regex pattern("\\((.*?)\\)");
// the source text
std::string text= "145(5)(7)";
const std::tr1::sregex_token_iterator end;
for (std::tr1::sregex_token_iterator i(text.begin(),
text.end(), pattern);
i != end;
++i)
{
std::cout << *i << std::endl;
}
I am getting the output as, 我得到的输出是,
(5) (5)
(7) (7)
I want the output without delimiters. 我想要没有定界符的输出。
Please help me to meet my requirement using STL TR1. 请帮助我满足使用STL TR1的要求。
You need to use sregex_iterator
instead of sregex_token_iterator
and then access the submatches via .str(n)
: 您需要使用
sregex_iterator
而不是sregex_token_iterator
,然后通过.str(n)
访问子sregex_token_iterator
:
const std::tr1::regex pattern1("\\((.*?)\\)");
std::string text= "145(5)(7)";
const std::tr1::sregex_iterator end;
for (std::tr1::sregex_iterator i(text.begin(),
text.end(), pattern1);
i != end;
++i)
{
std::cout << (*i).str(1) << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.