[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

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
0
Tom Knowlton
Asked:
Tom Knowlton
2 Solutions
 
existenz2Commented:
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);
0
 
Tom KnowltonWeb developerAuthor Commented:
Does not seem to be working....hmmmm.....
0
 
Tom KnowltonWeb developerAuthor Commented:
     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;
            }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dunglaCommented:
You must use \x000D for CR and \x000A for LF
0
 
Bob LearnedCommented:
Try this:

public string RemoveInvalidChars(string tempString)
          {
               string cleanedRecord = String.Empty;

               cleanedRecord = Regex.Replace(tempString,@"\\r","===");
               cleanedRecord = Regex.Replace(cleanedRecord,@"\\n","^^^");
         
               System.Diagnostics.Debug.WriteLine("RESULT:  " + cleanedRecord);
               return cleanedRecord;
          }

Bob
0
 
CeiledCommented:
The "@" in front of the string disables all escape sequences, which is why the examples above aren't working. This subtly-modified code will work:

            public string RemoveInvalidChars(string tempString)
            {
                  string cleanedRecord = String.Empty;

                  cleanedRecord = Regex.Replace(tempString, "\r", "===");
                  cleanedRecord = Regex.Replace(cleanedRecord, "\n", "^^^");

                  Console.WriteLine("RESULT:  " + cleanedRecord);
                  return cleanedRecord;
            }

Note that there's only one slash and no @ symbol.

Honestly, though, regexes are overkill for this. If you just want a simple string replacement, you should just use String.Replace(), like so:

            public static string RemoveInvalidChars(string tempString)
            {
                  return tempString.Replace("\n", "===").Replace("\r", "~~~");
            }

Regular expressions are great for complex string manipulation, but they're not a silver bullet, and it's easy to over-use them.
0
 
Bob LearnedCommented:
Agreed :)

Bob
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now