2014-09-20 19 views
0

我希望能找到一種方法,它符合以下標準兩個正則表達式組合:如何驗證開始和不開始使用正則表達式?

  1. 開頭的/ home/
  2. 不以/ home /指數

對於啓動第一種情況我認爲這是有效的:'^/home /'

我不確定第二種情況的正則表達式應該是什麼。

回答

1

如果支持,您可以實現負向預測。

^/home/(?!index).*$ 

說明:

^   # the beginning of the string 
/home/  # '/home/' 
(?!   # look ahead to see if there is not: 
    index  # 'index' 
)   # end of look-ahead 
.*   # any character except \n (0 or more times) 
$   # before an optional \n, and the end of the string 

Live Demo

+1

我認爲它不會匹配'/家庭/'字符串。 – 2014-09-20 17:36:53

+0

'+'運算符是一個錯字..修正.. – hwnd 2014-09-20 17:37:42

+0

@hwnd如果我想確保字符串不包含: – KKa 2014-09-20 18:39:18