ai didi

C++ 11 正则表达式

转载 作者:太空狗 更新时间:2023-10-29 20:36:46 24 4
gpt4 key购买 nike

如何使用 C++ 11 RegEx 在以下模式中查找“时间”:

"time = '1212232' or raisetime ='1212' or AlarmParameter1 = 'abc time cd'"

并将其转换为:

 "timeStamp = '1212232' or raisetime ='1212' or AlarmParameter1 = 'abc time cd'". 

它不应该影响其他“时间”字的其他出现。

最佳答案

尝试组合使用 std::string::findstd::string::replace

这得到了位置:

size_t f = s.find("time");

这将替换文本:

s.replace(f, std::string("time").length(), "timeStamp");

源代码: http://cpp.sh/3ljv

int main()
{
std::string name = "time = '1212232' or raisetime ='1212' or AlarmParameter1 = 'abc time cd'";
size_t f = name.find("time");
std::cout << name.replace(f, std::string("time").length(), "timeStamp");
}

输出:

timeStamp = '1212232' or raisetime ='1212' or AlarmParameter1 = 'abc time cd

如果你想替换所有出现的地方,你可以使用这样的函数:

bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}

replace(str, "time", "timeStamp");

关于C++ 11 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338060/

24 4 0
文章推荐: python - dir_util.copy_tree 在 shutil.rmtree 之后失败
文章推荐: c# - 使用 TypeConverter.ConvertFromString() 解析自定义格式的字符串
文章推荐: c# - Entity Framework Skip 方法运行速度很慢
文章推荐: python - 选择具有重量的随机项目
太空狗
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com