Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

RegEx

I need code in C#.

Code needs to use RegEx to replace all CR (carriage return) characters with "==="

and

replace all LF (line feed) characters with "^^^"



So this:

asdfasdfasdfCRLFasdfasdfasdfsadfCRLFasdfasdfsda


becomes:

asdfasdfasdf===^^^asdfasdfasdfsadf===^^^asdfasdfsda




Thanks!!!


Tom
Avatar of existenz2
existenz2
Flag of Netherlands image

This will do the trick:

using System.Text.RegularExpressions;
...
string text = "asdfasdfasdfCRLFasdfasdfasdfsadfCRLFasdfasdfsda";
Console.WriteLine(text);
string newText = Regex.Replace(Regex.Replace(text,"CR","==="),"LF","^^^");
Console.WriteLine(newText);
Avatar of Tom Knowlton

ASKER

Does not seem to be working....hmmmm.....
     public string RemoveInvalidChars(string tempString)
            {
                  string cleanedRecord = String.Empty;

                  cleanedRecord = Regex.Replace(tempString,@"\x0d","===");
                  cleanedRecord = Regex.Replace(cleanedRecord,@"\x0a","^^^");
            
                  System.Diagnostics.Debug.WriteLine("RESULT:  " + cleanedRecord);
                  return cleanedRecord;
            }
You must use \x000D for CR and \x000A for LF
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Agreed :)

Bob