Let’s say I have the subject:
////////File description////////
Name: SomeFile.cs
Size: 234
Description: Foo
Date: 08.14.2012
///////////////////////////////
how can I make that subject turn into:
////////File description////////
Name:
Size:
Description:
Date:
///////////////////////////////
Right now I do the following:
var pattern =
@"(/+File description/+
Name: )(?<name>.+)(
Size: )(?<size>.+)(
Description: )(?<des>.+)(
Date: )(?<date>.+)(
/+)";
// subject = fist code at top of this questoin
var temp = Regex.Replace(subject,pattern,"$1$2$3$4$5");
The pattern is very messy
Now my question is:
I will like to have the pattern:
/+File description/+
Name: (?<name>.+)
Size: (?<size>.+)
Description: (?<des>.+)
Date: (?<date>.+)
/+
I was wondering if it is possible to replace the groups name, size. etc with nothing
This may be more complex than you would like, but you could try using the MatchEvaluator. The MatchEvaluator computes the replacement string for each match. And the MatchEvaluator has access to the “Match” object, so it can do some interesting things, limited only by your imagination…
Another approach using the MatchEvaluator looks like this (and should work with your pattern or mine).
This works because you are replacing within a Match. I.e., your outer Match narrows down the search while your individual replacements have no chance of replacing the wrong thing. If you use this approach, your outer pattern can be much simpler because the groups are not needed.