2012-08-11 68 views
0

有沒有辦法將下面的所有內容與一個正則表達式匹配?如果不是,那麼幹淨的方法是什麼?將URL與特定數量的正斜槓匹配

/resources/$ 
/resources/type$ 
/resources/type/$ 
/resources/category$ 
/resources/category/$ 
/resources/anything here that includes up to 1 slash and ends with that slash 

以下應不匹配,因爲something存在,:

/resources/type/something 
/resources/category/something 

回答

0

您可以使用這個:

^\/resources(\/[a-z]*\/?)?$ 
+0

謝謝。我調整了也​​匹配首都,數字和破折號。 '^ \ /資源(\/[A-ZA-Z0-9 - ] * \ /)?$' – 2012-08-11 11:40:22

0

這應該做你想要什麼:

^\/([^\/]*\/?){,2}$ 

編輯的錯誤:對

+0

此錯誤匹配「/資源/測試/某物」 – 2012-08-11 10:55:59

+0

謝謝,但這也匹配以比'/資源/'其他的東西開始的URL,而我努力適應它。 – 2012-08-11 11:44:08

+1

是的。在我的辯護中,你的問題中沒有任何內容說/ url/resources /不應該被匹配(儘管你可以看到它的含義)。我會在「不應該匹配」部分下放置非資源/網址,以提供更清晰的問題。 – Amadan 2012-08-13 22:13:13