How do I remove words from a text file that have symbols preceding them?
For example:
This is important information... //but this is a comment
This is more important info... //and this is another comment
How do I remove the words along with the symbol “//but this is a comment” ?
Here’s my pseudocode:
1. If "//" is detected, line.replace "//" symbol
2. Clear the words after the symbol
3. Go on to the next line till you see "//" symbol
4. Repeat steps 1-3 (loop).
Note: this is occurring while the file is being read:
String line;
while ((line = textReader.readLine()) != null)
I’m assuming that given:
You want:
Something like this should work:
Patternis what Java uses for regular expressions. Here’s some information about Java regular expressions in Java. The regex I’ve used looks for two forward slashes and everything after that until the end of the line. Then, the text that is matched is replaced by an empty string.Pattern.DOTALLtells Java to treat^and$as beginning and end-of-line markers.EDIT
This code below demonstrates how it works: