【发布时间】:2016-05-04 02:06:02
【问题描述】:
我正在使用std::regex,需要进行搜索和替换。
我的字符串是:
begin foo even spaces and maybe new line(
some text only replace foo foo bar foo, keep the rest
)
some more text not replace foo here
只应触摸begin .... ( 和) 之间的内容。
我设法通过使用此搜索和替换替换了第一个 foo:
(begin[\s\S]*?\([\s\S]*?)foo([\s\S]*?\)[\s\S]*)
$1abc$2
但是,如何一次性替换所有三个 foo? 我尝试了环视,但由于量词而失败。
最终结果应如下所示:
begin foo even spaces and maybe new line(
some text only replace abc abc bar abc, keep the rest
)
some more text not replace foo here
问题更新:
我正在寻找一个纯正则表达式解决方案。也就是说,问题应该只通过更改the online C++ demo中的search和replace字符串来解决。
【问题讨论】:
-
最终结果应该是什么样子?
-
也许获取括号之间的文本并全部替换会更容易?然后你重建字符串。
-
@Thomas 有,仔细阅读。
-
@Revolver_Ocelot 该死的复制/粘贴错误。谢谢。