Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C#

Create Regex Objects using a Kind of "meta-variables" - Quicker and Easier

Rate me:
Please Sign up or sign in to vote.
3.83/5 (5 votes)
10 Jan 2007CPOL3 min read 32K   62   12  
This article describes a class VarRegex allowing you to reuse parts of regular expressions
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

/*
 * VarRegex class allows you to reuse parts of regular expressions
 * (C) Eugene MIrotin aka Guard, 2007
 */

namespace varRegex
{
    class VarRegex
    {
        public class VariablesCollection : IEnumerable<String>
        {
            public class ExpandedVariablesCollection : IEnumerable<String>
            {
                VariablesCollection variables;

                public ExpandedVariablesCollection(VariablesCollection variables)
                {
                    this.variables = variables;
                }

                public String this[String name]
                {
                    get
                    {
                        return variables.Expand(variables[name]);
                    }
                }

                public IEnumerator<String> GetEnumerator()
                {
                    foreach (String s in variables)
                        yield return variables.Expand(s);
                }

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


            }

            private Dictionary<String, String> variables;
            private ExpandedVariablesCollection expandedVariables;
            private VarRegex parent;

            public VariablesCollection(VarRegex parent)
            {
                variables = new Dictionary<string, string>();
                expandedVariables = new ExpandedVariablesCollection(this);
                this.parent = parent;
            }

            public String this[String name]
            {
                get 
                { 
                    if (variables.ContainsKey(name))
                        return variables[name];
                    else
                        throw new Exception("Invalid variable name");
                }
                set 
                { 
                    variables[name] = value;
                    parent.RegenRegex();
                }
            }

            public ExpandedVariablesCollection ExpandedVariables
            {
                get { return expandedVariables; }
            }
            
            public int Count
            {
                get { return variables.Count; }
            }

            public void Clear()
            {
                variables.Clear();
            }

            public IEnumerator<String> GetEnumerator()
            {
                foreach (String s in variables.Values)
                    yield return s;
            }

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

            public String Expand(String pattern)
            {
                if (pattern == "")
                    return "";

                string p = pattern;
                p = p.Replace("\\`", ""+(char)1);

                Regex r = new Regex("^`[^`]+`$");
                Match m = r.Match(p);
                if (m.Success)
                    return Expand(variables[p.Substring(1, p.Length - 2)]);

                r = new Regex("`([^`]+)`");
                MatchCollection ms = r.Matches(p);

                foreach (Match match in ms)
                {
                    string t = match.Groups[1].Value;
                    p = p.Replace("`" + t + "`", Expand(variables[t]));
                }

                p = p.Replace(""+(char)1, "`");

                return p;
            }
        }

        private VariablesCollection variables;
        private String pattern;
        private Regex regex;
        private RegexOptions options;

        public VarRegex()
        {
            regex = null;
            pattern = "";
            variables = new VariablesCollection(this);
            options = RegexOptions.None;
        }

        public RegexOptions Options
        {
            get { return options; }
            set 
            { 
                options = value;
                RegenRegex();
            }
        }

        public VariablesCollection Variables
        {
            get { return variables; }
        }

        public String Pattern
        {
            get { return pattern; }
            set 
            { 
                pattern = value;
                RegenRegex();
            }
        }

        public Regex Regex
        {
            get { return regex; }
        }

        private void RegenRegex()
        {
            regex = new Regex(variables.Expand(pattern), options);
        }
        

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Belarus Belarus
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions