Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / XML

.NET Regular Expressions Find and Replace Add-In for Visual Studio 2008

Rate me:
Please Sign up or sign in to vote.
4.91/5 (36 votes)
12 Oct 2009CPOL3 min read 185.4K   1.6K   117  
A .NET Regular Expressions Find and Replace add-in for Visual Studio 2008
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RegexFindAndReplace
{
    /// <summary>
    /// The MatchContextUtils class exposes several static methods that provide information about a match.
    /// </summary>
    class MatchContextUtils
    {
        const char NEW_LINE_CHARACTOR = '\n';

        /// <summary>
        /// Determines whether the specified source has multiple lines.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>
        /// 	<c>true</c> if the specified source has multiple lines; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasMultipleLines( string source )
        {
            return GetLineCount( source ) > 0;
        }

        /// <summary>
        /// Gets the line count.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>The number of lines</returns>
        public static int GetLineCount( string source )
        {
            return GetLineCount( source, source.Length - 1 );
        }

        /// <summary>
        /// Gets the line count.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="endIndex">The end index.</param>
        /// <returns>The number of lines before the end index</returns>
        public static int GetLineCount( string source, int endIndex )
        {
            int count = 1;

            if ( source != null && source.Length != 0 )
            {
                count = Regex.Matches( source.Substring( 0, endIndex ), "\n" ).Count + 1;
            }

            return count;
        }

        /// <summary>
        /// Gets the match context string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="endIndex">The end index.</param>
        /// <returns></returns>
        public static string GetMatchContextString( string source, int startIndex, int endIndex )
        {
            return GetMatchContextString( source, startIndex, endIndex, 0, 0 );
        }

        /// <summary>
        /// Gets the match context string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="endIndex">The end index.</param>
        /// <param name="matchContextBeforeLineCount">The match context before line count.</param>
        /// <param name="matchContextAfterLineCount">The match context after line count.</param>
        /// <returns></returns>
        public static string GetMatchContextString( string source, int startIndex, int endIndex, int matchContextBeforeLineCount, int matchContextAfterLineCount )
        {
            string context = string.Empty;

            int contextStartPosition = source.LastIndexOf( NEW_LINE_CHARACTOR, startIndex );

            if ( contextStartPosition < 0 )
            {
                contextStartPosition = 0;
            }

            if ( matchContextBeforeLineCount > 0 && contextStartPosition > 0 )
            {
                for ( int i = 1; i <= matchContextBeforeLineCount && contextStartPosition > 0; i++ )
                {
                    int newLinePosition = source.LastIndexOf( NEW_LINE_CHARACTOR, contextStartPosition - 1 );

                    if ( newLinePosition > 0 )
                    {
                        contextStartPosition = newLinePosition;
                    }
                    else
                    {
                        contextStartPosition = 0;
                    }
                }
            }

            int contextEndPosition = source.IndexOf( NEW_LINE_CHARACTOR, endIndex );
            
            if ( contextEndPosition < 0 )
            {
                contextEndPosition = source.Length;
            }

            if ( matchContextAfterLineCount > 0 && contextEndPosition < source.Length )
            {
                for ( int i = 1; i <= matchContextAfterLineCount && contextEndPosition < source.Length; i++ )
                {
                    int newLinePosition = source.IndexOf( NEW_LINE_CHARACTOR, contextEndPosition + 1 );

                    if ( newLinePosition > 0 )
                    {
                        contextEndPosition = newLinePosition;
                    }
                    else
                    {
                        contextEndPosition = source.Length;
                    }
                }
            }

            if ( contextStartPosition == 0 )
            {
                context = source.Substring( 0, contextEndPosition - contextStartPosition );
            }
            else
            {
                context = source.Substring( contextStartPosition + 1, contextEndPosition - contextStartPosition );
            }

            return context;
        }

        /// <summary>
        /// Gets the match context string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="endIndex">The end index.</param>
        /// <param name="matchContextBeforeLineCount">The match context before line count.</param>
        /// <param name="matchContextAfterLineCount">The match context after line count.</param>
        /// <returns></returns>
        public static string GetReplacementContextString( string source, int startIndex, int endIndex, string replacement, int matchContextBeforeLineCount, int matchContextAfterLineCount )
        {
            string context = string.Empty;

            int contextStartPosition = source.LastIndexOf( NEW_LINE_CHARACTOR, startIndex );

            if ( contextStartPosition < 0 )
            {
                contextStartPosition = 0;
            }

            if ( matchContextBeforeLineCount > 0 && contextStartPosition > 0 )
            {
                for ( int i = 1; i <= matchContextBeforeLineCount && contextStartPosition > 0; i++ )
                {
                    int newLinePosition = source.LastIndexOf( NEW_LINE_CHARACTOR, contextStartPosition - 1 );

                    if ( newLinePosition > 0 )
                    {
                        contextStartPosition = newLinePosition;
                    }
                    else
                    {
                        contextStartPosition = 0;
                    }
                }
            }

            int contextEndPosition = source.IndexOf( NEW_LINE_CHARACTOR, endIndex );

            if ( contextEndPosition < 0 )
            {
                contextEndPosition = source.Length;
            }

            if ( matchContextAfterLineCount > 0 && contextEndPosition < source.Length )
            {
                for ( int i = 1; i <= matchContextAfterLineCount && contextEndPosition < source.Length; i++ )
                {
                    int newLinePosition = source.IndexOf( NEW_LINE_CHARACTOR, contextEndPosition + 1 );

                    if ( newLinePosition > 0 )
                    {
                        contextEndPosition = newLinePosition;
                    }
                    else
                    {
                        contextEndPosition = source.Length;
                    }
                }
            }

            if ( contextStartPosition == 0 )
            {
                context = string.Format( "{0}{1}{2}",
                    source.Substring( 0, startIndex ),
                    replacement,
                    source.Substring( endIndex, contextEndPosition - endIndex ) );
            }
            else
            {
                context = string.Format( "{0}{1}{2}", 
                    source.Substring( contextStartPosition + 1, startIndex - contextStartPosition - 1 ),
                    replacement, 
                    source.Substring( endIndex, contextEndPosition - endIndex ) );
            }

            return context;
        }

        /// <summary>
        /// Gets the position in the specified source line.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="absolutePosition">The absolute position.</param>
        /// <returns></returns>
        public static int GetPositionInLine( string source, int absolutePosition )
        {
            int position = 0;
            int newlinePosition = source.LastIndexOf( NEW_LINE_CHARACTOR, absolutePosition );

            if ( newlinePosition == -1 )
            {
                position = absolutePosition + 1;
            }
            else
            {
                if ( absolutePosition == newlinePosition )
                {
                    newlinePosition = source.LastIndexOf( NEW_LINE_CHARACTOR, absolutePosition - 1 );
                }

                position = absolutePosition - newlinePosition;
            }

            return position;
        }
    }
}

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
United States United States
I am a software developer currently working in Salt Lake City, Utah. I work primarily with C# for my job, but I mess around with C, Perl, and Windows PowerShell for fun.

Comments and Discussions