Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4078752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:42:50+00:00 2026-05-20T17:42:50+00:00

The loop: var pattern = _dict[key]; string before; do { before = pattern; foreach

  • 0

The loop:

var pattern = _dict[key];
string before;
do
{
    before = pattern;
    foreach (var pair in _dict)
        if (key != pair.Key)
            pattern = pattern.Replace(string.Concat("{", pair.Key, "}"), string.Concat("(", pair.Value, ")"));
} while (pattern != before);
return pattern;

It just does a repeated find-and-replace on a bunch of keys. The dictionary is just <string,string>.

I can see 2 improvements to this.

  1. Every time we do pattern.Replace it searches from the beginning of the string again. It would be better if when it hit the first {, it would just look through the list of keys for a match (perhaps using a binary search), and then replace the appropriate one.
  2. The pattern != before bit is how I check if anything was replaced during that iteration. If the pattern.Replace function returned how many or if any replaces actually occured, I wouldn’t need this.

However… I don’t really want to write a big nasty thing class to do all that. This must be a fairly common scenario? Are there any existng solutions?


Full Class

Thanks to Elian Ebbing and ChrisWue.

class FlexDict : IEnumerable<KeyValuePair<string,string>>
{
    private Dictionary<string, string> _dict = new Dictionary<string, string>();
    private static readonly Regex _re = new Regex(@"{([_a-z][_a-z0-9-]*)}", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    public void Add(string key, string pattern)
    {
        _dict[key] = pattern;
    }

    public string Expand(string pattern)
    {
        pattern = _re.Replace(pattern, match =>
            {
                string key = match.Groups[1].Value;

                if (_dict.ContainsKey(key))
                    return "(" + Expand(_dict[key]) + ")";

                return match.Value;
            });

        return pattern;
    }

    public string this[string key]
    {
        get { return Expand(_dict[key]); }
    }

    public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
    {
        foreach (var p in _dict)
            yield return new KeyValuePair<string,string>(p.Key, this[p.Key]);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Example Usage

class Program
{
    static void Main(string[] args)
    {
        var flex = new FlexDict
            {
                {"h", @"[0-9a-f]"},
                {"nonascii", @"[\200-\377]"},
                {"unicode", @"\\{h}{1,6}(\r\n|[ \t\r\n\f])?"},
                {"escape", @"{unicode}|\\[^\r\n\f0-9a-f]"},
                {"nmstart", @"[_a-z]|{nonascii}|{escape}"},
                {"nmchar", @"[_a-z0-9-]|{nonascii}|{escape}"},
                {"string1", @"""([^\n\r\f\\""]|\\{nl}|{escape})*"""},
                {"string2", @"'([^\n\r\f\\']|\\{nl}|{escape})*'"},
                {"badstring1", @"""([^\n\r\f\\""]|\\{nl}|{escape})*\\?"},
                {"badstring2", @"'([^\n\r\f\\']|\\{nl}|{escape})*\\?"},
                {"badcomment1", @"/\*[^*]*\*+([^/*][^*]*\*+)*"},
                {"badcomment2", @"/\*[^*]*(\*+[^/*][^*]*)*"},
                {"baduri1", @"url\({w}([!#$%&*-\[\]-~]|{nonascii}|{escape})*{w}"},
                {"baduri2", @"url\({w}{string}{w}"},
                {"baduri3", @"url\({w}{badstring}"},
                {"comment", @"/\*[^*]*\*+([^/*][^*]*\*+)*/"},
                {"ident", @"-?{nmstart}{nmchar}*"},
                {"name", @"{nmchar}+"},
                {"num", @"[0-9]+|[0-9]*\.[0-9]+"},
                {"string", @"{string1}|{string2}"},
                {"badstring", @"{badstring1}|{badstring2}"},
                {"badcomment", @"{badcomment1}|{badcomment2}"},
                {"baduri", @"{baduri1}|{baduri2}|{baduri3}"},
                {"url", @"([!#$%&*-~]|{nonascii}|{escape})*"},
                {"s", @"[ \t\r\n\f]+"},
                {"w", @"{s}?"},
                {"nl", @"\n|\r\n|\r|\f"},

                {"A", @"a|\\0{0,4}(41|61)(\r\n|[ \t\r\n\f])?"},
                {"C", @"c|\\0{0,4}(43|63)(\r\n|[ \t\r\n\f])?"},
                {"D", @"d|\\0{0,4}(44|64)(\r\n|[ \t\r\n\f])?"},
                {"E", @"e|\\0{0,4}(45|65)(\r\n|[ \t\r\n\f])?"},
                {"G", @"g|\\0{0,4}(47|67)(\r\n|[ \t\r\n\f])?|\\g"},
                {"H", @"h|\\0{0,4}(48|68)(\r\n|[ \t\r\n\f])?|\\h"},
                {"I", @"i|\\0{0,4}(49|69)(\r\n|[ \t\r\n\f])?|\\i"},
                {"K", @"k|\\0{0,4}(4b|6b)(\r\n|[ \t\r\n\f])?|\\k"},
                {"L", @"l|\\0{0,4}(4c|6c)(\r\n|[ \t\r\n\f])?|\\l"},
                {"M", @"m|\\0{0,4}(4d|6d)(\r\n|[ \t\r\n\f])?|\\m"},
                {"N", @"n|\\0{0,4}(4e|6e)(\r\n|[ \t\r\n\f])?|\\n"},
                {"O", @"o|\\0{0,4}(4f|6f)(\r\n|[ \t\r\n\f])?|\\o"},
                {"P", @"p|\\0{0,4}(50|70)(\r\n|[ \t\r\n\f])?|\\p"},
                {"R", @"r|\\0{0,4}(52|72)(\r\n|[ \t\r\n\f])?|\\r"},
                {"S", @"s|\\0{0,4}(53|73)(\r\n|[ \t\r\n\f])?|\\s"},
                {"T", @"t|\\0{0,4}(54|74)(\r\n|[ \t\r\n\f])?|\\t"},
                {"U", @"u|\\0{0,4}(55|75)(\r\n|[ \t\r\n\f])?|\\u"},
                {"X", @"x|\\0{0,4}(58|78)(\r\n|[ \t\r\n\f])?|\\x"},
                {"Z", @"z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z"},
                {"Z", @"z|\\0{0,4}(5a|7a)(\r\n|[ \t\r\n\f])?|\\z"},

                {"CDO", @"<!--"},
                {"CDC", @"-->"},
                {"INCLUDES", @"~="},
                {"DASHMATCH", @"\|="},
                {"STRING", @"{string}"},
                {"BAD_STRING", @"{badstring}"},
                {"IDENT", @"{ident}"},
                {"HASH", @"#{name}"},
                {"IMPORT_SYM", @"@{I}{M}{P}{O}{R}{T}"},
                {"PAGE_SYM", @"@{P}{A}{G}{E}"},
                {"MEDIA_SYM", @"@{M}{E}{D}{I}{A}"},
                {"CHARSET_SYM", @"@charset\b"},
                {"IMPORTANT_SYM", @"!({w}|{comment})*{I}{M}{P}{O}{R}{T}{A}{N}{T}"},
                {"EMS", @"{num}{E}{M}"},
                {"EXS", @"{num}{E}{X}"},
                {"LENGTH", @"{num}({P}{X}|{C}{M}|{M}{M}|{I}{N}|{P}{T}|{P}{C})"},
                {"ANGLE", @"{num}({D}{E}{G}|{R}{A}{D}|{G}{R}{A}{D})"},
                {"TIME", @"{num}({M}{S}|{S})"},
                {"PERCENTAGE", @"{num}%"},
                {"NUMBER", @"{num}"},
                {"URI", @"{U}{R}{L}\({w}{string}{w}\)|{U}{R}{L}\({w}{url}{w}\)"},
                {"BAD_URI", @"{baduri}"},
                {"FUNCTION", @"{ident}\("},
            };

        var testStrings = new[] { @"""str""", @"'str'", "5", "5.", "5.0", "a", "alpha", "url(hello)", 
            "url(\"hello\")", "url(\"blah)", @"\g", @"/*comment*/", @"/**/", @"<!--", @"-->", @"~=",
            "|=", @"#hash", "@import", "@page", "@media", "@charset", "!/*iehack*/important"};

        foreach (var pair in flex)
        {
            Console.WriteLine("{0}\n\t{1}\n", pair.Key, pair.Value);
        }

        var sw = Stopwatch.StartNew();
        foreach (var str in testStrings)
        {
            Console.WriteLine("{0} matches: ", str);
            foreach (var pair in flex)
            {
                if (Regex.IsMatch(str, "^(" + pair.Value + ")$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture))
                    Console.WriteLine("  {0}", pair.Key);
            }
        }
        Console.WriteLine("\nRan in {0} ms", sw.ElapsedMilliseconds);
        Console.ReadLine();
    }
}

Purpose

For building complex regular expressions that may extend eachother. Namely, I’m trying to implement the css spec.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:42:50+00:00Added an answer on May 20, 2026 at 5:42 pm

    I think it would be faster if you look for any occurrences of {foo} using a regular expression, and then use a MatchEvaluator that replaces the {foo} if foo happens to be a key in the dictionary.

    I have currently no visual studio here, but I guess this is functionally equivalent with your code example:

    var pattern = _dict[key];
    bool isChanged = false;
    
    do
    {
        isChanged = false;
    
        pattern = Regex.Replace(pattern, "{([^}]+)}", match => {
            string matchKey = match.Groups[1].Value;
    
            if (matchKey != key && _dict.ContainsKey(matchKey))
            {
                isChanged = true;
                return "(" + _dict[matchKey] + ")";
            }
    
            return match.Value;
        });
    } while (isChanged);
    

    Can I ask you why you need the do/while loop? Can the value of a key in the dictionary again contain {placeholders} that have to be replaced? Can you be sure you don’t get stuck in an infinite loop where key "A" contains "Blahblah {B}" and key "B" contains "Blahblah {A}"?

    Edit: further improvements would be:

    • Using a precompiled Regex.
    • Using recursion instead of a loop (see ChrisWue’s comment).
    • Using _dict.TryGetValue(), as in Guffa’s code.

    You will end up with an O(n) algorithm where n is the size of the output, so you can’t do much better than this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i have a while loop: var i = 0; while(i < 20) {
So I create multiple BackgroundWorkers using loop: foreach (var item in list) { worker
This is what my loop looks like: var loopResult = Parallel.ForEach(folder.Items.Cast<object>(), (item, loopState) =>
I have a loop and I need to return certain numbers in a pattern
I have this foreach loop: var includedElements = new HashSet<int>(); foreach(var e in elements)
Following is my code for Ajax request: for(some loop) { var SOME-VARIABLE = value;
this is my code snippet, where the program doesn't enter the foreach loop: var
I am running a simple loop: var c=0; while(c<count){ theimg = $container.find('.img'+c+''); $thisX =
I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,
I have the following loop: for(var myScreen in wizardScreens){ if(step==index)$(myScreen).show(); else $(myScreen).hide(); index++; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.