Click here to Skip to main content
14,773,757 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hello

I believe that there is a better way to use RegEx in C# than I have done below:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    Regex r = new Regex("{.+}");
    var matches = r.Matches(String);
    var replacer = new StringBuilder(String);
    foreach (var match in matches)
    {
        var newMatch = match.ToString().Substring(1, match.ToString().Length - 2);
        var value = new StaticResourceExtension(newMatch).ProvideValue(serviceProvider);
        replacer.Replace(match.ToString(), value.ToString());
    }
    return replacer.ToString();
}


What I have tried:

The code above is what I am currently using. But I think there is better..
Posted
Updated 1-Mar-18 8:09am

1 solution

Something like this should work:
private static readonly Regex Pattern = new Regex("{(.+?)}", RegexOptions.Compiled);

public override object ProvideValue(IServiceProvider serviceProvider)
{
    return Pattern.Replace(String, match =>
    {
        string newMatch = match.Groups[1].Value;
        var value = new StaticResourceExtension(newMatch).ProvideValue(serviceProvider);
        return Convert.ToString(value);
    });
}
   
v2
Comments
Medtronic WPF Developer 1-Mar-18 14:35pm
   
Thanks so much I knew there was something, but I completely forgot (it was years ago) and it was hard to find the answer since most information on RegEx is the basic stuff, which is still hard to remember unless you work with it all the time. I figured there was some brain out there I could pick and appreciate that you took the time to answer.
Medtronic WPF Developer 1-Mar-18 15:28pm
   
You did make a couple of mistakes, but you led me in the right direction. This is the right code in the middle:
return Pattern.Replace(String, match =>
{
var str = match.Groups[0].Value;
var provideStr = str.Substring(1, str.Length - 2);
var value = new StaticResourceExtension(provideStr).ProvideValue(serviceProvider);
return Convert.ToString(value);
});
Richard Deeming 1-Mar-18 15:34pm
   
No; if you copy the pattern from my answer, match.Groups[1].Value will give you the same thing as your substring code. :)

Grouping Constructs - Regular Expression Language - Quick Reference | Microsoft Docs[^]
Richard Deeming 1-Mar-18 15:40pm
   
The one problem you might run into is the "greedy" + operator. If you have multiple matches to replace, it will end up processing the whole substring from the first { to the last }.

Change the expression to "{(.+?)}", and you'll get each match individually.

Demo[^]
Maciej Los 1-Mar-18 16:05pm
   
5ed!
Medtronic WPF Developer 6-Mar-18 19:45pm
   
Thanks, I fixed it, and works great.
Clifford Nelson 4-Mar-18 12:43pm
   
Have some things I have not used in this solution. Solid thumbs up.

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