Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 10 Jan 2007 CPOL
This article describes a class VarRegex allowing you to reuse parts of regular expressions
varregex_src.zip
VarRegex
varRegex
Properties
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)

Share

About the Author

Eugene Mirotin (Guard)
Software Developer
Belarus Belarus
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 Jan 2007
Article Copyright 2007 by Eugene Mirotin (Guard)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid