正则表达式,以匹配在Java中不包含特定字符串的字符串
我需要一个正则表达式,它匹配string/*exa*/mple*/,中的子字符串 匹配的字符串必须是/*exa*/not/*exa*/mple*/ 它也不能包含*/在其中 我试过这些正则表达式: /\\*[.*&&[^*/]]\\*/ , /\\*.*&&?!^*/$\\*/ 但我无法得到确切的解决方案。您可以尝试以下方法:正则表达式,以匹配在Java中不包含特定字符串的字符串,java,Java,我需要一个正则表达式,它匹配string/*exa*/mple*/,中的子字符串 匹配的字符串必须是/*exa*/not/*exa*/mple*/ 它也不能包含*/在其中 我试过这些正则表达式: /\\*[.*&&[^*/]]\\*/ , /\\*.*&&?!^*/$\\*/ 但我无法得到确切的解决方案。您可以尝试以下方法: /\*[^\*\/\*]+\*/ --> anything that is in between (including) "/*" and "*/" 以下是
/\*[^\*\/\*]+\*/ --> anything that is in between (including) "/*" and "*/"
以下是一个示例:
Pattern p = Pattern.compile("/\\*[^\\*\\/\\*]+\\*/");
Matcher m = p.matcher("/*exa*/mple*/");
while (m.find()){
System.out.println(m.group());
}
输出:
/*埃克萨*/
我知道你想从文本中挑出评论
Pattern p = Pattern.compile("/\\*.*?\\*/");
Matcher m = p.matcher("/*ex*a*/mple*/and/*more*/ther*/");
while (m.find()){
System.out.println(m.group());
}
尝试最简单的方法是使用两个正则表达式,匹配rexexp1&&~REGEP2