I would like to regex/replace the following set:
[something] [nothing interesting here] [boring.....]
by
%0 %1 %2
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?
PHP and Perl both support a 'callback' replacement, allowing you to hook some code into generating the replacement. Here's how you might do it in PHP with preg_replace_callback
You could do this with a global var and a regular function callback, but wrapping it up in class is a little neater.
This is possible with regex in C#, as
Regex.Replace
can take a delegate as a parameter.Not directly since what you are describing has a procedural component. I think Perl might allow this though its qx operator (I think) but in general you need to loop over the string which should be pretty simple.
You can use
Regex.Replace
, it has a handy overload that takes a callback: