Very simplistic style add or update In my library for styling the Console I need to be able to add or update two styles: color and background-color.

The use case is very simple. There is a string with styles that I'm building and that I update to change the color if necessary. 

I divided this task into three steps: 


explode the string into declarations
manipulate the style
reconstruct the style


Example Each of these steps has its own extension:

void Main()
{
    var style = "color: red;";

    style
        .ToDeclarations()
        .AddOrUpdate(
            ("color", "black"),
            ("background-color", "blue")
        )
        .ToStyle()
        .Dump();
}


These extensions are used by higher-level extensions that call them with const string for property names. I just used simple strings in LINQPad.



Code Their implementation is very straightforward. They use only LINQ and tuples.

public static class CssExtensions
{
    public static IEnumerable&lt;(string property, string value)&gt; ToDeclarations(
        this string style)
    {
        const string declarationPattern = @"(?&lt;property&gt;[a-z-]+):\s*(?&lt;value&gt;.+?)(;|$)";

        return
            Regex
                .Matches(style, declarationPattern, RegexOptions.IgnoreCase)
                .Cast&lt;Match&gt;()
                .Select(m =&gt; (
                    property: m.Value("property").Trim(),
                    value: m.Value("value").Trim())
                );
    }

    public static IEnumerable&lt;(string property, string value)&gt; AddOrUpdate(
        this IEnumerable&lt;(string property, string value)&gt; declarations,
        params (string property, string value)[] updates)
    {
        return
            declarations
                .Concat(updates)
                .GroupBy(t =&gt; t.property)
                .Select(t =&gt; t.Last())
                .Skip(t =&gt; string.IsNullOrEmpty(t.value));
    }

    public static string ToStyle(
        this IEnumerable&lt;(string property, string value)&gt; declarations)
    {
        return
            declarations
                .Select(t =&gt; $"{t.property}: {t.value};")
                .Join(" ");
    }
}


As the use case is very simple and I also don't need to parse any complex styles so are the extensions, just a simple regex with some string manipulation.

I update the style by grouping by the property and pick the last one if there are mutliple ones, if there is none yet, then a new style is added. If the value happens to be null or Empty then it's removed.



There are three more extensions: one that I use to Join declarations, one Value shortcut for regex and a Skip that I use as an inverse of Where to use positive conditions if I want to exclude some cases.

public static class EnumerableExtensions
{   
    public static string Join&lt;T&gt;(this IEnumerable&lt;T&gt; values, string separator)
    {
        return string.Join(separator, values);
    }

    public static IEnumerable&lt;T&gt; Skip&lt;T&gt;(this IEnumerable&lt;T&gt; source, Func&lt;T, bool&gt; predicate)
    {
        return source.Where(x =&gt; !predicate(x));
    }
}

public static class MatchExtensions
{
    public static string Value(this Match match, string groupName)
    {
        return match.Groups[groupName].Value;
    }
}




Should I still improve it in any way?
The only thing that pops out to me (though admittedly, it is very minor) is that you're recompiling the regex every time you call ToDeclarations. 

You could use a static Regex instance which compiles the expression only once. Regex instances are also threadsafe - so no worries there.

Thus, your code could look like:

private static Regex _cssRegex = new Regex("(?&lt;property&gt;[a-z-]+):\s*(?&lt;value&gt;.+?)(;|$)", RegexOptions.IgnoreCase);
public static IEnumerable&lt;(string property, string value)&gt; ToDeclarations(this string style)
{
    return
        _cssRegex
            .Matches(style)
            .Cast&lt;Match&gt;()
            .Select(m =&gt; (
                property: m.Value("property").Trim(),
                value: m.Value("value").Trim())
            );
}