Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / Visual Basic

Visual Studio Add-in for testing regular expressions

Rate me:
Please Sign up or sign in to vote.
4.94/5 (17 votes)
1 Dec 2010CPOL4 min read 39.6K   474   36  
An add-in for debugging and creating regular expression directly in Visual Studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.CodeDom;
using System.Text.RegularExpressions;
using System.Globalization;
using System.CodeDom.Compiler;

namespace RegexTester.Parsing.CSharp
{
    public class CSharpRegexFindResults : IRegexFindResults
    {
        public bool FoundMatch { get; set; }
        public bool IsMatchOnlyRegex { get; set; }
        public string ToMatch { get; set; }
        public string ToMatchExpression { get; set; }
        public string ReplacePattern { get; set; }
        public string Regex { get; set; }
        public string RegexNamespace { get; set; }
        public RegexOptions RegexOptions { get; set; }
        public bool IsEdit { get; set; }
        public int Index { get; set; }
        public int Length { get; set; }
        public CodeDomProvider CodeDomProvider { get; set; }

        public void ConvertToEdit()
        {
            if (!IsEdit)
            {
                if (ToMatch != null)
                {
                    ToMatch = DisplayToEditString(ToMatch, true);
                }
                if (Regex != null)
                {
                    Regex = DisplayToEditString(Regex, true);
                }
                if (ReplacePattern != null)
                {
                    ReplacePattern = DisplayToEditString(ReplacePattern, true);
                }
                IsEdit = true;
            }
        }

        public void ConvertToDisplay()
        {
            if (IsEdit)
            {
                if (ToMatch != null)
                {
                    ToMatch = EditToDisplayString(ToMatch, true);
                }
                if (Regex != null)
                {
                    Regex = EditToDisplayString(Regex, true);
                }
                if (ReplacePattern != null)
                {
                    ReplacePattern = EditToDisplayString(ReplacePattern, true);
                }
                IsEdit = false;
            }
        }

        public string GetRegexString()
        {
            if (!IsEdit)
            {
                throw new InvalidOperationException("Cannot convert while not in edit mode");
            }
            StringBuilder result = new StringBuilder();
            if (IsMatchOnlyRegex)
            {
                result.Append("@\"");
                result.Append(Regex);
                result.Append("\"");
            }
            else
            {
                if (!FoundMatch)
                {
                    if (ReplacePattern == null)
                    {
                        result.Append("new Regex");
                    }
                    else
                    {
                        result.Append("Regex.Replace");
                    }
                }
                result.Append("(");
                result.Append(ToMatch != null ? string.Format(CultureInfo.InvariantCulture, "@\"{0}\", ", ToMatch) : "");
                if (ToMatchExpression != null)
                {
                    result.Append(ToMatchExpression);
                    result.Append(", ");
                }
                result.Append("@\"");
                result.Append(Regex);
                result.Append("\"");
                result.Append(ReplacePattern != null ? string.Format(CultureInfo.InvariantCulture, ", @\"{0}\"", ReplacePattern) : "");
                if (RegexOptions != RegexOptions.None)
                {
                    result.Append(", ");
                    result.Append(string.Join(" | ",
                        from int v in Enum.GetValues(typeof(RegexOptions))
                        where v > 0 && (RegexOptions & ((RegexOptions)v)) == ((RegexOptions)v)
                        select string.Format(CultureInfo.InvariantCulture, "{0}RegexOptions.{1}", RegexNamespace, Enum.GetName(typeof(RegexOptions), v))));
                }
                result.Append(")");
            }
            return result.ToString();
        }

        public string EditToDisplayString(string value, bool isVerbatim)
        {
            if (isVerbatim)
            {
                value = value.Replace("\"\"", "\"");
            }
            else
            {
                value = value.Replace(@"\""", @"""");
            }
            return value;
        }

        public string DisplayToEditString(string value, bool isVerbatim)
        {
            if (isVerbatim)
            {
                value = value.Replace("\"", "\"\"");
            }
            else
            {
                StringWriter stringWriter = new StringWriter();
                CodeDomProvider.GenerateCodeFromExpression(new CodePrimitiveExpression(value), stringWriter, null);
                value = stringWriter.ToString();
                value = value.Substring(1, value.Length - 2);
            }
            return value;
        }
    }
}

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 (Senior)
Denmark Denmark
.NET developer. I wanted to be first an astronaut, then a jet pilot, but when I got a Commodore 64 for Christmas I never looked back. Also I would never have qualified for the first two things and everybody knows computer programmers get all the girls.

Comments and Discussions