为指定函数的每次出现返回带引号的字符串 arg
return quoted string arg for every occurence of a specified function
我在 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
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 从python中调用C++函数并获取返回值
- 为指定函数的每次出现返回带引号的字符串 arg