2014-04-12 15 views
-1

大家好我剛開始的正則表達式和執行的示例代碼找到正則表達式的功能在Java中

package practice; 

import java.util.regex.*; 

public class Regexpr2 { 



    public static void main(String args[]) 
    { 
     Pattern pat = Pattern.compile("Java 7 naya hai") ; 
     Matcher match = pat.matcher("Java"); 
     System.out.println("Here I am ------------>"); 
     if (match.find()) 
      System.out.println("got it using find method---------"); 
     else 
      System.out.println("didnt got"); 

    } 
} 

但我不是我所期待的輸出

enter code here 

輸出:

這裏我是------------> 沒有得到

有人可以解釋爲什麼是「else」語句是開始摹excuted

+0

請確保您的問題在發佈之前格式正確。謝謝。 –

回答

1

模式必須匹配String,而不是倒過來

private static final Pattern pattern = Pattern.compile("Java"); 

注意的子序列:你不需要使用使用,因爲您選擇正則表達式簡單Strings

if ("Java 7 naya hai".contains("Java")) { 
    ... 
} 
+0

謝謝你的回答reimeus.Got我的錯誤。 –