Skip to main content
 首页 » 编程设计

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

2024年01月18日34mayingbao

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

[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'