正则表达式替换有越来越多的 [英] regex replacement with a increasing number
本文介绍了正则表达式替换有越来越多的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想正则表达式/替换以下设置:
I would like to regex/replace the following set:
[something] [nothing interesting here] [boring.....]
按
%0 %1 %2
在换句话说,是建立与任何EX pressions []
将成为%
跟随受到越来越多......
In other words, any expressions that is built with []
will become a %
followed by an increasing number...
是否有可能做一个正则表达式,对吗?
Is it possible to do it right away with a Regex?
推荐答案
这是可能的正则表达式在C#中,如 Regex.Replace
可以采取委托作为参数。
This is possible with regex in C#, as Regex.Replace
can take a delegate as a parameter.
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'
这篇关于正则表达式替换有越来越多的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文