Click here to Skip to main content
15,885,278 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:
C#
string pattern = @"/[a-z]{2}/products/\d{4}/\(?<fileName>[abc])\.aspx";


C#
string inputString = longString(around 3000 characters);

but i need to utilise this capture to pass into a constructor
C#
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
C#
"info.aspx?number" + guid;


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

1 solution

Regex.Replace has an overload that takes a match evaluator method:
C#
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();
    }
 
Share this answer
 
Comments
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