[英]Regex match from pound symbol to end of line
我试图使用regexr.com提出解决方案,但找不到。 我正在寻找一些与此匹配的正则表达式:
#This is some text \n
我想匹配整行的地方,它从井号符号开始,到换行符结束。 例如:
#This is some text \n
This is some text I don't want to match
我想匹配第一行,以便完全删除它。 我正在使用Java Regex引擎。
编辑:这是我尝试过的:
/(#.*\n)/g
删除所有注释行以及它们的换行符(如果存在)。
string.replaceAll("(?m)^#.*\n?", "");
没有可选的量词?
,此(?m)^#.*\\n
正则表达式不会删除最后一条注释行。
您可以使用(?m)^#.*\\n
作为模式:
String lines =
"#This is some text \n" +
"#This is some text \n" +
"This is some text I don't want to match\n";
String comment_removed = lines.replaceAll("(?m)^#.*\n", "");
使用(?m)
(多行模式)使^
在行的开头匹配。 否则,它将仅在字符串的开头匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.