Regex.Replace and Switch Statement???

Posted on 2006-05-12
Last Modified: 2008-02-01
I'm using the following code to Regex.Replace all Alpha characters from a very long alphanumeric string with a zero.

            string pattern = @"[^\d+]"; //"[A-Za-z:-]";
            return Regex.Replace(input, pattern, "0");

Q. Is there a method of replacing each alpha character with a different substitute?

For example: A or a = 1, B or b = 2, : = 0, etc............

I'm thinking about a C# swich statement, however I thought the EE might know a nice Regex.Replace trick. :-)
Question by:kvnsdr
    LVL 62

    Accepted Solution

    Hi kvnsdr;

    The following code will assign a different number to each of the 26 alpha characters 1 thru 26 and the : to a value of 0. You can modify the sample code to fit your needs

          // Test input string
          string input = "A:bcdefghijk123lmno456pqrs789tuv000wxY:Z";      
          // Regex pattern. Note the location of the + meta character
          // do not place it in the character class, inside the buckets.
          string pattern = @"[^\d]+";
          string output;
          output = Regex.Replace(input, pattern, new MatchEvaluator(ReplaceAlphas));

    private string ReplaceAlphas( Match m )
          // Get the characters to replace
          char[] chars = m.Value.ToUpper().ToCharArray();
          // Return this string
          string retStr = "";
          // Convert the input
          for( int idx = 0; idx < chars.Length; idx++ )
                if( chars[idx] == ':' )
                      // Replace the : character to a 0
                      retStr += "0";
                      // Replace the alphas with a number, A|a = 1, B|b = 2
                      // C|c = 3, ..., Z|z = 26. The hex value of a A character
                      // is 0x41 to Z which is 0x5A. Subtracting a hex 0x40 will
                      // give us the values 1 - 26 decmial.
                      retStr += (chars[idx] - 0x40).ToString();      
                      return retStr;

    LVL 96

    Expert Comment

    by:Bob Learned
    You know, you could do this without a regular expression.


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now