Click here to Skip to main content
14,826,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wish to make a capture using RegEx
following pattern:
string pattern = @"/[a-z]{2}/products/\d{4}/\(?<fileName>[abc])\.aspx";

string inputString = longString(around 3000 characters);

but i need to utilise this capture to pass into a constructor
Object obj = new Obj(capture)

in order to load the object and retrieve a property (guid) of that object, obviously this guid will be different for every match.

i need to do this for every match on the regEx pattern, and then replace input string with
"info.aspx?number" + guid;

and then return the input string with all replacements done.
Updated 25-Nov-14 5:00am

1 solution

Regex.Replace has an overload that takes a match evaluator method:
string output = Regex.Replace(input, pattern, ReplaceMethod);

private string ReplaceMethod(Match m)
    MyObject obj = new MyObject(m.Value);
    return "info.aspx?number" + obj.Guid.ToString();
Grant Weatherston 25-Nov-14 11:58am
thanks alot, don't know how i'm going to pass the language into the delegate though :/
OriginalGriff 25-Nov-14 12:09pm
No, a MatchEvaluator method can only take one parameter - but surely it can pick up the language from the class? It can't change between matches in the same input string, can it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900