简体   繁体   English

查找并替换为正则表达式

[英]Find and replace with regular expressions

I'm trying to replace a bunch of function calls using regular expressions but can't seem to be getting it right. 我正在尝试使用正则表达式替换一堆函数调用,但似乎无法正确处理。 This is a simplified example of what I'm trying to do: 这是我要执行的操作的简化示例:

GetPetDog();
GetPetCat();
GetPetBird();

I want to change to: 我想更改为:

GetPet<Animal_Dog>();
GetPet<Animal_Cat>();
GetPet<Animal_Bird>();

Use below regex: 在正则表达式下使用:

(GetPet)([^(]*) with subsitution \1<Animal_\2>

Demo 演示版

You can use the following regex and code for that: 您可以为此使用以下正则表达式和代码:

std::string ss ("GetPetDog();");
static const std::regex ee ("GetPet([^()]*)");
std::string result;
result = regex_replace(ss, ee, "GetPet<Animal_$1>");
std::cout << result << endl;

Regex : 正则表达式

  • GetPet - Matches GetPet literally (we need no capturing group here) GetPet从字面上匹配GetPet (此处无需捕获组)
  • ([^()]*) - A capturing group to match any characters other than ( or ) 0 or more times ( * ) ([^()]*) -一个捕获组,以匹配除()以外的任何其他字符0次或更多次( *

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM