2014-09-05 40 views

回答

2

嘗試以下正則表達式:

"\\w+\\s*[-+*]\\s*\\w+" 
2

您不需要轉義字符類中存在的字符+,*。當存在前一個和後一個標記時,您必須轉義-符號。

^\w+\s*[+*-]\s*\w+$ 

Java的正則表達式將是

^\\w+\\s*[+*-]\\s*\\w+$ 

你不需要在使用匹配函數把錨。

實施例:

String xxx = "\\w+\\s*[+*-]\\s*\\w+"; 
String x = "wordA + wordB"; 
if(x.matches(xxx)) { 
     System.out.println("matches"); 
} 
else { 
      System.out.println("no match found "); 
} // matches 
相關問題