Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

Help a guy out with RegEx

OK, I get how to replace a group in Regex.Replace something like this:

Regex.Replace(@"(blah)(blah2)(blah3)", "new$2new", RegexOptions.IgnoreCase)

...where the result is "newblah2new"

But how would use replace "blah2" with something dynamically generated, namely another string based on "blah2"? I'm tripping all over the examples in the docs and need a little help.
Posted: Oct 20 2004, 11:15 AM by Jeff | with 5 comment(s)
Filed under:

Comments

Andrey Skvortsov said:

Look at:
public static string Replace(
string input,
string pattern,
string replacement,
RegexOptions options
);
# October 20, 2004 11:41 AM

Andrey Skvortsov said:

Sorry,doc error "string replacement," is "MatchEvaluator evaluator,"
# October 20, 2004 11:42 AM

Alain Tésio said:

You can concatenate the string, just be careful to escape characters with have a specific meaning for regular expressions:
pattern = String.Format("(blah)(%s)(blah3)",Regex.Escape(somethingDunamicallyGenerated"));
# October 20, 2004 1:14 PM

Jon Galloway said:

Jeff -
Dunno if you got this working yet. Andrey's comment on the MatchEvaluator is the way to go if you're doing something dynamic based on your regex match. I've used this in altering HTML refrences and it's worked well. Each match calls into the MatchEvaluator delegate which can do just about anything.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextregularexpressionsregexclassreplacetopic5.asp

- Jon
# October 23, 2004 5:51 AM

Eric Newton said:

match evaluator is the way to go, i've used it several times
# October 25, 2004 3:14 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)