Goal is to take in a text file, normalize it down to only having all upper case letters, remove all special characters, and turn any new line into a single space.
This is my current messy code to do it, and as far as I can tell it does work.
public string readTextFile(string fileName)
{
Regex rgx = new Regex("[^A-Z ]");
string txtFile = File.ReadAllText(fileName).ToUpper();
txtFile = Regex.Replace(txtFile, @"\s+", " ", RegexOptions.Multiline);
return rgx.Replace(txtFile, "");
}
Looking for anyone to help clean this code up, improve efficiency, and possibly combine my regex statements to one.
fullBookhere?\s+matches 1 or more any whitespace. If you need to match newlines only, use[\r\n]+. How many files do you handle? 1, 10, 100000? If more than a hundred, I suggest that you should avoid declaring the Regex object every time, declare it outside the method (best asprivate static readonlyfield).