C++ 为指定函数的每次出现返回带引号的字符串arg
我正在C++11x模式下使用UbuntuLinux14.04和g++4.9.2,并尝试使用 我也可以使用boostregex 为清晰起见,需要对该字符串进行解析,并删除引号上的转义符: 1+MyFunfoo-3>MyFunx2_1:3+MyFun 10.0.0.30:80/b3.new-MyFun2z:2/OldMyFunblue 请注意允许不同间距的要求,并避免部分匹配MyFun,例如不匹配OldMyFunblue 我需要将不带引号的参数返回到字符串中MyFun的每次出现,而不返回MyFun。对于上述示例: 福 x2_1:3 10.0.0.30:80/b3.新 但这些都不是: 1. 3. z:2 蓝色 我有这个正则表达式,但它返回MyFun以及所需和引用的参数:C++ 为指定函数的每次出现返回带引号的字符串arg,c++,regex,c++11,g++,c++-standard-library,C++,Regex,C++11,G++,C++ Standard Library,我正在C++11x模式下使用UbuntuLinux14.04和g++4.9.2,并尝试使用 我也可以使用boostregex 为清晰起见,需要对该字符串进行解析,并删除引号上的转义符: 1+MyFunfoo-3>MyFunx2_1:3+MyFun 10.0.0.30:80/b3.new-MyFun2z:2/OldMyFunblue 请注意允许不同间距的要求,并避免部分匹配MyFun,例如不匹配OldMyFunblue 我需要将不带引号的参数返回到字符串中MyFun的每次出现,而不返回MyFun。
const std::string regexString="(MyFun\\()+(\"([^\"]*)\")\\)";
但只需要不带引号的arg。我使用的是std::regex和std::sregex_迭代器
#include <string>
#include <regex>
#include <iostream>
int main()
{
const std::string str="(1+MyFun(\"foo\" )-\"3\" ) > (MyFun (\"x2_1:3\")"
"+MyFun( \"10.0.0.30:80/b3.new\" ) - MyFun2(\"z:2\")/OldMyFun(\"blue\")";
const std::string regexString="MyFun\\s*\\(\\s*\"([^\"]+)\"\\s*\\)";
try
{
std::regex rgx(regexString);
std::sregex_iterator i(str.begin(), str.end(), rgx);
std::sregex_iterator e;
while (i != e)
{
std::cout<<i->str()<<std::endl;
++i;
}
}
catch(const std::exception& e)
{
std::cerr<<e.what()<<std::endl;
}
}
或
试试这个。抓取捕获或组1。参见演示
使用此?:MyFun\\+\[^\]*\\\然后打印组索引1。我在测试程序中尝试了此操作,但得到的输出为零。这将返回MyFun作为答案的一部分,但只返回arg。@user2138025 grab group 1I在我的测试程序中尝试了\\1,但它什么也没有返回。@user2138025 try'$1',我认为这不是正确的c++11x std::regex语法。可以在上面的代码片段中尝试您的建议吗?
MyFun\s*\(\s*"([^"]+)"\s*\)
MyFun\\s*\\(\\s*\"([^\"]+)\"\\s*\\)