Removing words after a symbol is detected
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 开发者_StackOverflowanother 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:
This is important information... //but this is a comment
This is more important info... //and this is another comment
You want:
This is important information...
This is more important info...
Something like this should work:
Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
line = matcher.replaceFirst("");
Pattern
is 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.DOTALL
tells Java to treat ^
and $
as beginning and end-of-line markers.
EDIT
This code below demonstrates how it works:
import java.util.regex.*;
public class RemoveComments {
public static void main(String[] args){
String[] lines = {"This is important information... //but this is a comment", "This is more important info... //and this is another comment"};
Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL);
for(String line : lines) {
Matcher matcher = pattern.matcher(line);
System.out.println("Original: " + line);
line = matcher.replaceFirst("");
System.out.println("New: " + line);
}
}
}
Just throwing an idea, you could play around with the functions of String
first locate the charaters that removes
int i = indexOf('//', 0);
Then look for the index of the next space
secondIndex = indexOf(' ',i);
then you can extract both side
String s1 = subString(0,i);
String s2 = subString(secondIndex,i);
String res = s1+s2;
This is not optimal but should get the job done ^^
You could use String.replaceAll()
to do a regular expression replacement in one line:
line = line.replaceAll("//.*$", "");
精彩评论