正则表达式匹配Java中不包含特定字符串的字符串

时间:2015-11-12 17:17:12

标签: java

我需要一个匹配字符串/*exa*/mple*/中的子字符串的正则表达式,

匹配的字符串必须为/*exa*/而不是/*exa*/mple*/

它也不得包含"*/"

我试过这些正则表达式:

  1. "/\\*[.*&&[^*/]]\\*/"
  2. "/\\*.*&&(?!^*/$)\\*/"
  3. 但我无法得到确切的解决方案。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

/\*[^\*\/\*]+\*/   --> 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());
    }

<强>输出:

  

/ * * EXA /

答案 1 :(得分:0)

我知道您想从文本中挑选评论。

Pattern p = Pattern.compile("/\\*.*?\\*/");
Matcher m = p.matcher("/*ex*a*/mple*/and/*more*/ther*/");
while (m.find()){
    System.out.println(m.group());
}
相关问题