为指定函数的每次出现返回带引号的字符串 arg

return quoted string arg for every occurence of a specified function

本文关键字:返回 arg 字符串 函数      更新时间:2023-10-16

我在 C++11x 模式下使用 Ubuntu Linux 14.04 和 g++ 4.9.2 并尝试使用std::regex(我也可以使用boost regex(。

需要解析此字符串(为清楚起见,删除了引号上的转义(:

"(1+MyFun("foo" )-"3" ) > (MyFun ("x2_1:3")+MyFun( "10.0.0.30:80/b3.new" ) - MyFun2("z:2")/OldMyFun("blue")">

请注意允许各种间距并避免部分匹配MyFun的要求(例如,不匹配OldMyFun("蓝色"(。

我需要将不带引号的参数返回到字符串中每次出现的 MyFun,而无需 MyFun。对于上面的示例:

  • 噗��
  • x2_1:3
  • 10.0.0.30:80/b3.新

但这些都不是:

  • 1
  • 3
  • z:2

我有这个正则表达式,但它返回 MyFun 以及所需的(和引用的(参数:

const std::string regexString="(MyFun\()+("([^"]*)")\)";

但只需要未引用的参数。我正在使用标准::正则表达式和标准::sregex_iterators。

#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;
    }
}
MyFuns*(s*"([^"]+)"s*)

MyFun\s*\(\s*"([^"]+)"\s*\)

试试这个。抓住捕获或组 1.参见演示。

https://regex101.com/r/vN3sH3/8