Regex sed需要像append的backreference这样的东西 我有一个C++源文件,我希望插入一个简单的函数定义(大约6行)。该定义应紧跟在另一个函数定义之后

Regex sed需要像append的backreference这样的东西 我有一个C++源文件,我希望插入一个简单的函数定义(大约6行)。该定义应紧跟在另一个函数定义之后,regex,sed,Regex,Sed,使用这个问题的公认答案:,我可以插入平凡的函数定义,但我无法将其作为适当类的成员进行定义 测试代码: void object::func1() { std::cout << "func1" << std::endl; } void对象::func1() { std::cout反向引用只能引用同一表达式中的捕获。!b后面的分号结束第一个表达式。保留空间可以将字符串从一个表达式传递到另一个表达式 sed'/\w\+::func1()$/,/^}/!b/\w\+::f

使用这个问题的公认答案:,我可以插入平凡的函数定义,但我无法将其作为适当类的成员进行定义

测试代码:

void object::func1()
{
    std::cout << "func1" << std::endl;
}
void对象::func1()
{

std::cout反向引用只能引用同一表达式中的捕获。
!b
后面的分号结束第一个表达式。保留空间可以将字符串从一个表达式传递到另一个表达式

sed'/\w\+::func1()$/,/^}/!b/\w\+::func1()$/{h;s/^\w*\s\+\(\w\+\)::func1()$/\1/;x};/^}/{g;s/*/}\n\n&::func2()\n{\n\\\\return 0;\n}/}'testcode.cc

Sed一次读取一行到模式空间中,像
s///
这样的命令在其中运行。 可以在保留空间中留出行,稍后再检索回模式空间

sed '
  /\w\+::func1()$/,/^}/!b   # Ignore lines outside the target function.
  /\w\+::func1()$/ {        # On the line declaring the function,
    h                       # save that line to the hold space;
    s/^\w*\s\+\(\w\+\)::func1()$/\1/  # replace the function with its class name;
    x                       # save the class name and restore the function declaration.
  }
  /^}/ {                    # at the end of the target function
    g                       # retrieve the class name
    # substitue the class name into the new function
    s/.*/}\n\nint &::func2()\n{\n\ \ \ \ return 0;\n}/
  }
' testcode.cc

反向引用只能引用同一表达式中的捕获。
后面的分号!b
结束第一个表达式。保持空间可以将字符串从一个表达式传递到另一个表达式

sed'/\w\+::func1()$/,/^}/!b/\w\+::func1()$/{h;s/^\w*\s\+\(\w\+\)::func1()$/\1/;x};/^}/{g;s/*/}\n\n&::func2()\n{\n\\\\return 0;\n}/}'testcode.cc

Sed一次读取一行到模式空间中,像
s///
这样的命令在其中运行。 可以在保留空间中留出行,稍后再检索回模式空间

sed '
  /\w\+::func1()$/,/^}/!b   # Ignore lines outside the target function.
  /\w\+::func1()$/ {        # On the line declaring the function,
    h                       # save that line to the hold space;
    s/^\w*\s\+\(\w\+\)::func1()$/\1/  # replace the function with its class name;
    x                       # save the class name and restore the function declaration.
  }
  /^}/ {                    # at the end of the target function
    g                       # retrieve the class name
    # substitue the class name into the new function
    s/.*/}\n\nint &::func2()\n{\n\ \ \ \ return 0;\n}/
  }
' testcode.cc

反向引用只能引用同一表达式中的捕获。
后面的分号!b
结束第一个表达式。保持空间可以将字符串从一个表达式传递到另一个表达式

sed'/\w\+::func1()$/,/^}/!b/\w\+::func1()$/{h;s/^\w*\s\+\(\w\+\)::func1()$/\1/;x};/^}/{g;s/*/}\n\n&::func2()\n{\n\\\\return 0;\n}/}'testcode.cc

Sed一次读取一行到模式空间中,像
s///
这样的命令在其中运行。 可以在保留空间中留出行,稍后再检索回模式空间

sed '
  /\w\+::func1()$/,/^}/!b   # Ignore lines outside the target function.
  /\w\+::func1()$/ {        # On the line declaring the function,
    h                       # save that line to the hold space;
    s/^\w*\s\+\(\w\+\)::func1()$/\1/  # replace the function with its class name;
    x                       # save the class name and restore the function declaration.
  }
  /^}/ {                    # at the end of the target function
    g                       # retrieve the class name
    # substitue the class name into the new function
    s/.*/}\n\nint &::func2()\n{\n\ \ \ \ return 0;\n}/
  }
' testcode.cc

反向引用只能引用同一表达式中的捕获。
后面的分号!b
结束第一个表达式。保持空间可以将字符串从一个表达式传递到另一个表达式

sed'/\w\+::func1()$/,/^}/!b/\w\+::func1()$/{h;s/^\w*\s\+\(\w\+\)::func1()$/\1/;x};/^}/{g;s/*/}\n\n&::func2()\n{\n\\\\return 0;\n}/}'testcode.cc

Sed一次读取一行到模式空间中,像
s///
这样的命令在其中运行。 可以在保留空间中留出行,稍后再检索回模式空间

sed '
  /\w\+::func1()$/,/^}/!b   # Ignore lines outside the target function.
  /\w\+::func1()$/ {        # On the line declaring the function,
    h                       # save that line to the hold space;
    s/^\w*\s\+\(\w\+\)::func1()$/\1/  # replace the function with its class name;
    x                       # save the class name and restore the function declaration.
  }
  /^}/ {                    # at the end of the target function
    g                       # retrieve the class name
    # substitue the class name into the new function
    s/.*/}\n\nint &::func2()\n{\n\ \ \ \ return 0;\n}/
  }
' testcode.cc
这可能适用于您(GNU-sed):

这将查找函数定义,然后在保持空间(HS)中构建普通函数。遇到函数结尾时,它会附加HS。

这可能适用于您(GNU-sed):

这将查找函数定义,然后在保持空间(HS)中构建普通函数。遇到函数结尾时,它会附加HS。

这可能适用于您(GNU-sed):

这将查找函数定义,然后在保持空间(HS)中构建普通函数。遇到函数结尾时,它会附加HS。

这可能适用于您(GNU-sed):



这将查找函数定义,然后在保持空间(HS)中构建普通函数。遇到函数结尾时,它会附加HS。

我不明白您想做什么,但是
不会破坏任何东西。相反,必须避免所有的垃圾斜杠:<代码> \ \ \ \ /< /C> >试图在C++源文件中逐个插入一个方法,并尝试从第一个方法捕获C++类名称,使第二个方法具有范围。我不明白您要做什么,但是<代码>,<代码>不会破坏任何东西。相反,必须避免所有的垃圾斜杠:<代码> \ \ \ \ /< /C> >试图在C++源文件中逐个插入一个方法,并尝试从第一个方法捕获C++类名称,使第二个方法具有范围。我不明白您要做什么,但是<代码>,<代码>不会破坏任何东西。相反,必须避免所有的垃圾斜杠:<代码> \ \ \ \ /< /C> >试图在C++源文件中逐个插入一个方法,并尝试从第一个方法捕获C++类名称,使第二个方法具有范围。我不明白您要做什么,但是<代码>,<代码>不会破坏任何东西。相反,必须避免所有的垃圾斜杠:<代码> \ \ \ \ /< /COD>试图在C++源文件中逐个插入一个方法,并尝试从第一个方法捕获C++类名,使第二个方法具有范围。请您简要说明一下吗?我添加了一个解释,但对于记录,Poton的解决方案更干净、更简单。我不知道
/
重用了最新的正则表达式。谢谢你的解释。我对这件事有点困惑!b、 认为这与跳转到标签有关。。。但它是单词边界的正则表达式。。正确的?。。所以不是单词边界。。。有什么事吗?*?
反转匹配,因此只有当正则表达式(或范围或替换)不匹配时才会发生
b
。没有标签的
b
分支到脚本的末尾,实际上忽略了当前行。单词边界模式是
\b
。你能提供一个简单的分类吗?我添加了一个解释,但为了记录在案,波彤的解决方案更干净、更简单。我不知道
/
重用了最新的正则表达式。谢谢你的解释。我对这件事有点困惑!b、 认为这与跳转到标签有关。。。但它是单词bo的正则表达式