基本上是这样的:
int abc = 10;
* this is a comment
因此,我只想找到* this is a comment
以便可以将其从字符串中删除。我尝试了一些示例,尽管它似乎不起作用。这个想法基本上是从*
单词,数字或符号(实际上是任何东西)的组合开始,然后以换行符结尾(\n)
。
谢谢。
该模式^\*.*$
应在这里工作:
String line = "Not a comment\n* This is a comment\n*This is also a comment\n";
line += "Not a * comment.";
String pattern = "^\\*.*$";
Pattern r = Pattern.compile(pattern, Pattern.MULTILINE);
Matcher m = r.matcher(line);
while (m.find( )) {
System.out.println("Found value: " + m.group(0) );
}
Found value: * This is a comment
Found value: *This is also a comment
除了*
正则表达式元字符外,这里无需多解释,如果您要将其用作文字,则需要使用反斜杠对其进行转义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句