gpt4 book ai didi

c# - 正则表达式替换越来越多

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:55 25 4
gpt4 key购买 nike

我想正则表达式/替换以下集合:

[something] [nothing interesting here] [boring.....]

经过
%0 %1 %2

换句话说,任何使用 [] 构建的表达式将成为 %随之而来的是越来越多的...

是否可以使用正则表达式立即完成?

最佳答案

这可以通过 C# 中的正则表达式来实现,如 Regex.Replace可以将委托(delegate)作为参数。

        Regex theRegex = new Regex(@"\[.*?\]");
string text = "[something] [nothing interesting here] [boring.....]";
int count = 0;
text = theRegex.Replace(text, delegate(Match thisMatch)
{
return "%" + (count++);
}); // text is now '%0 %1 %2'

关于c# - 正则表达式替换越来越多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4367405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com