2015-11-12 55 views
0

特定字符串的字符串,我需要在字符串匹配/*exa*/mple*/一個子正則表達式,正則表達式匹配不包含在Java中

匹配的字符串必須/*exa*//*exa*/mple*/

它也不能包含其中的"*/"

我嘗試了這些正則表達式:

  1. "/\\*[.*&&[^*/]]\\*/"
  2. "/\\*.*&&(?!^*/$)\\*/"

,但即時通訊無法得到確切的解決方案。

+0

試試這個[如何找到在Java中使用正則表達式的確切的詞?(http://stackoverflow.com/questions/9464261/how-to-find-the-exact-word-using-a -regex-in-java) – Perdomoff

+0

最簡單的方法是使用兩個正則表達式,匹配rexexp1 &&〜regexp2 –

回答

0

我知道你想從文本中挑選評論。

Pattern p = Pattern.compile("/\\*.*?\\*/"); 
Matcher m = p.matcher("/*ex*a*/mple*/and/*more*/ther*/"); 
while (m.find()){ 
    System.out.println(m.group()); 
} 
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()); 
    } 

OUTPUT:

/* * EXA/