MichelleLacy
asked on
Search for text and delete from file
How do I search for a string of text in a file, using wild card, and then delete the entire line from the file....
if line contains "My name is *", delete line from file
if line contains "My name is *", delete line from file
Hi
try the following code (I'm assuming your not opening text files over 10s of megabytes):
try the following code (I'm assuming your not opening text files over 10s of megabytes):
string filePath = "your file path";
string fileContent = File.ReadAllText(filePath);
fileContent = Regex.Replace(fileContent, "My name is.*", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
File.WriteAllText("your file path", fileContent);
iHadi, your code doesn't delete the whole line.
How about this modification:
How about this modification:
string filePath = "file path";
string fileContent = File.ReadAllText(filePath);
String[] lines = fileContent.Split('\n');
fileContent = "";
foreach(String line in lines)
{
if (! Regex.Match(line, ".*My name is.*", RegexOptions.IgnoreCase).Success)
{
fileContent += line + '\n';
}
}
File.WriteAllText(filePath, fileContent);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@tculler: Good solution, reading and writing at the same time. I think I saw an example of that in ddayx10's link. I would still recommend to use regexes though (to allow for future modifications). This code would probably need to have .ToLower() on every line and "My name is ".
I never check links, and question askers usually don't either. If they're asking a question, they should have already researched using such obvious resources.
I didn't do a ToLower(), or use regex, because I felt that from the question asked the problem was specific enough to target in on one thing, do that one thing, and do it very well. RegEx is awesome, but does require a bit of overhead when using it (though it will save time, and probably boost efficiency, in more complex situations). As long as you know this won't change, this solution is fairly optimized and expandable. Of course, if there is more content you must avoid, RegEx may become a necessity (though you would just replace the call to .Contains to a Regex call). Let me know if you'd like to see the implementation of my solution using RegEx, and I'll whip one up.
I didn't do a ToLower(), or use regex, because I felt that from the question asked the problem was specific enough to target in on one thing, do that one thing, and do it very well. RegEx is awesome, but does require a bit of overhead when using it (though it will save time, and probably boost efficiency, in more complex situations). As long as you know this won't change, this solution is fairly optimized and expandable. Of course, if there is more content you must avoid, RegEx may become a necessity (though you would just replace the call to .Contains to a Regex call). Let me know if you'd like to see the implementation of my solution using RegEx, and I'll whip one up.
https://www.experts-exchange.com/questions/21696282/Delete-line-from-text-file.html