C# Regex - Replace multiple characters at once without overwriting?

Posted by Everaldo Aguiar on Stack Overflow See other posts from Stack Overflow or by Everaldo Aguiar
Published on 2010-06-13T09:00:44Z Indexed on 2010/06/13 9:12 UTC
Read the original article Hit count: 471

Filed under:
|
|
|
|

Hello guys, I'm implementing a c# program that should automatize a Mono-alphabetic substitution cipher. The functionality i'm working on at the moment is the simplest one: The user will provide a plain text and a cipher alphabet, for example:

Plain text(input): THIS IS A TEST

Cipher alphabet: A -> Y, H -> Z, I -> K, S -> L, E -> J, T -> Q

Cipher Text(output): QZKL KL QJLQ

I thought of using regular expressions since I've been programming in perl for a while, but I'm encountering some problems on c#. First I would like to know if someone would have a suggestion for a regular expression that would replace all occurrence of each letter by its corresponding cipher letter (provided by user) at once and without overwriting anything.

Example:

In this case, user provides plaintext "TEST", and on his cipher alphabet, he wishes to have all his T's replaced with E's, E's replaced with Y and S replaced with J. My first thought was to substitute each occurrence of a letter with an individual character and then replace that character by the cipherletter corresponding to the plaintext letter provided.

Using the same example word "TEST", the steps taken by the program to provide an answer would be:

1 - replace T's with (lets say) @

2 - replace E's with #

3 - replace S's with &

4 - Replace @ with E, # with Y, & with j

5 - Output = EYJE

This solution doesn't seem to work for large texts. I would like to know if anyone can think of a single regular expression that would allow me to replace each letter in a given text by its corresponding letter in a 26-letter cipher alphabet without the need of splitting the task in an intermediate step as I mentioned.

If it helps visualize the process, this is a print screen of my GUI for the program: http://img43.imageshack.us/img43/2118/11618743.jpg

© Stack Overflow or respective owner

Related posts about c#

Related posts about regex