java正则表达式检测\n
我正在尝试删除字符串中的LaTeX注释: 输入字符串: \begin{comment}在内部\n comment 1\end{comment}在comment之外的东西\begin{comment}在comment 2之内\end{comment}在comment 2之后 输出: \在注释1内开始{comment}\在注释2后的注释外结束{comment} 理想产出:java正则表达式检测\n,java,regex,latex,newline,replaceall,Java,Regex,Latex,Newline,Replaceall,我正在尝试删除字符串中的LaTeX注释: 输入字符串: \begin{comment}在内部\n comment 1\end{comment}在comment之外的东西\begin{comment}在comment 2之内\end{comment}在comment 2之后 输出: \在注释1内开始{comment}\在注释2后的注释外结束{comment} 理想产出: something outside comments after comment 2 示例代码: public static
something outside comments after comment 2
示例代码:
public static void main(String[] args) {
String input = "\\begin{comment}inside \n comment 1 \\end{comment} something outside comments \\begin{comment} inside comment 2\\end{comment} after comment 2";
System.out.println(input.replaceAll("\\\\begin\\{comment\\}(.*|[\\s]*|\\n*)\\\\end\\{comment\\}", ""));
}
所以问题是这个正则表达式没有检测到\n
我使用以下链接来形成正则表达式:
使用选项编译您的
模式
,或者将等效的标志表达式(?s)
添加到您的正则表达式中,以便
匹配\n
。此外,如果您的正则表达式似乎不起作用,请尝试以下操作:
System.out.println(input.replaceAll("(?s)\\\\begin\\{comment\\}.*?\\\\end\\{comment\\}", ""));
使用该选项编译
模式
,或者将等效的标志表达式(?s)
添加到正则表达式中,以便
匹配\n
。此外,如果您的正则表达式似乎不起作用,请尝试以下操作:
System.out.println(input.replaceAll("(?s)\\\\begin\\{comment\\}.*?\\\\end\\{comment\\}", ""));
System.out.println(input.replaceAll((?m)\\\\begin\\{comment\\}(.*.[124;[\\ s]*\\\\\n*)\\\\\\end\\{comment\\},“”);给出了相同的结果…还尝试了模式p=Pattern.compile(\\\\begin\\{comment\\}(.*\\\[\\s]*\\\\\n*)\\\\end\\{comment\});System.out.println(input.replaceAll(p.toString(),“”);System.out.println(input.replaceAll((?m)\\\\begin\\{comment\\}(.*.[124;[\\ s]*\\\\\n*)\\\\\\end\\{comment\\},“”);给出了相同的结果…还尝试了模式p=Pattern.compile(\\\\begin\\{comment\\}(.*\\\[\\s]*\\\\\n*)\\\\end\\{comment\});System.out.println(input.replaceAll(p.toString(),“”);