Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Regex.Replace and Switch Statement???

Posted on 2006-05-12
Medium Priority
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 64

Accepted Solution

Fernando Soto earned 1000 total points
ID: 16674500
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
ID: 16677678
You know, you could do this without a regular expression.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

564 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