Skip to main content
added 766 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

I'm not saying I like this but you could reuse the longest overload for the shorter ones. Here I took the one that takes three parameters.

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x));
}

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x, y));
}

Here's another small experiment with C# 7 where you could use anonymous tuples with only one method. But actually you could return a tuple in you current solution too.

public static class RegexStringExtensions
{   
    public static string PatternReplace(
        this string seed, 
        string pattern,
        Func<(string, string, string), string> outPattern
    )
    {
        return Regex.Replace(
            seed, 
            pattern, 
            m => outPattern((
                m.Groups[1].Value, 
                m.Groups[2].Value, 
                m.Groups[3].Value)
            )
        );
    }
}

Usage:

string val = "abcdef".PatternReplace(
    @"(ab)(cd)(ef)", 
    t => t.Item1 + string.Concat(t.Item2.Reverse())
);

I'm not saying I like this but you could reuse the longest overload for the shorter ones. Here I took the one that takes three parameters.

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x));
}

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x, y));
}

I'm not saying I like this but you could reuse the longest overload for the shorter ones. Here I took the one that takes three parameters.

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x));
}

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x, y));
}

Here's another small experiment with C# 7 where you could use anonymous tuples with only one method. But actually you could return a tuple in you current solution too.

public static class RegexStringExtensions
{   
    public static string PatternReplace(
        this string seed, 
        string pattern,
        Func<(string, string, string), string> outPattern
    )
    {
        return Regex.Replace(
            seed, 
            pattern, 
            m => outPattern((
                m.Groups[1].Value, 
                m.Groups[2].Value, 
                m.Groups[3].Value)
            )
        );
    }
}

Usage:

string val = "abcdef".PatternReplace(
    @"(ab)(cd)(ef)", 
    t => t.Item1 + string.Concat(t.Item2.Reverse())
);
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

I'm not saying I like this but you could reuse the longest overload for the shorter ones. Here I took the one that takes three parameters.

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x));
}

public static string PatternReplace(
    this string seed, 
    string pattern,
    Func<string, string, string> outPattern
)
{
    return seed.PatternReplace(pattern, (x, y, z) => outPattern(x, y));
}