Replace string with regular expression : Match « Regular Expressions « C# / C Sharp






Replace string with regular expression

    
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Utils.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The utils.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.DataAccess
{
    #region Imports

    using System;
    using System.Web;

    #endregion

    /// <summary>
    /// The utils.
    /// </summary>
    public class Utils
    {
        #region [rgn] Methods (2)

        /// <summary>
        /// The replace ex.
        /// </summary>
        /// <param name="original">
        /// The original.
        /// </param>
        /// <param name="pattern">
        /// The pattern.
        /// </param>
        /// <param name="replacement">
        /// The replacement.
        /// </param>
        /// <returns>
        /// The replace ex.
        /// </returns>
        public static string ReplaceEx(string original, string pattern, string replacement)
        {
            int count, position0, position1;
            count = position0 = position1 = 0;
            string upperString = original.ToUpper();
            string upperPattern = pattern.ToUpper();
            int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length);
            var chars = new char[original.Length + Math.Max(0, inc)];
            while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
            {
                for (int i = position0; i < position1; ++i)
                {
                    chars[count++] = original[i];
                }

                for (int i = 0; i < replacement.Length; ++i)
                {
                    chars[count++] = replacement[i];
                }

                position0 = position1 + pattern.Length;
            }

            if (position0 == 0)
            {
                return original;
            }

            for (int i = position0; i < original.Length; ++i)
            {
                chars[count++] = original[i];
            }

            return new string(chars, 0, count);
        }

       
        #endregion [rgn]
    }
}

   
    
    
    
  








Related examples in the same category

1.Regular expressions: MatchRegular expressions: Match
2.Use regualr expression to check if a string is a GUID
3.Count lines in a string with regular expression
4.Validates the phone number with regular expression
5.Validates the area code with regular expression
6.Validates the exchange code with regular expression
7.Validates the local 4 digit phone number with regular expression
8.Get Area Code by using Regular expression
9.Format Phone Number
10.Clean Telephone Number with Regular expression
11.Remove Nulls with Regex
12.Provides static validation methods for strings.
13.Checks if name matches pattern with '?' and '*' wildcards.