C++ で boost::regex を使用して検索用語を抽出する方法、特に用語に html エンティティが含まれている場合。
例えば
p=test&test&sort=price
Search Term would be 'test&test'
p=test&test&sort=price
Search Term would be 'test&test'
ブースト正規表現コードは次のとおりです
bool regexExtract(const string &strInput,string &strOutput,const string& regex)
{
bool succ = false;
if ( ! strOutput.empty() )
{
boost::regex re(regex, boost::regex::perl);//,boost::regex::perl | boost:regex::icase);
boost::sregex_iterator res(strInput.begin(),strInput.end(),re);
boost::sregex_iterator end;
for (; res != end; ++res)
cout << (*res)[0] << std::endl;
}
return succ;
}
正規表現をstring re = "p=(([^&;#]|(&.*?;))*).*";
使用すると、以下に出力されます
p=test&test&sort=asc
perlの同じ正規表現は完全に正常に機能します
echo "p=test&asd;test&sort=asc" | perl -ne 'if ( $_ =~ /p=(([^\&;#]|(&.*?;))*).*/){print $1;}'
test&asd;test