2010-04-22 159 views
1

獲取的內容我想 「the-game」 使用正則表達式的網址,像什麼位於之間正則表達式:從URL

  • http://www.somesite.com.domain.webdev.domain.com/en/the-game/another-one/another-one/another-one/
  • http://www.somesite.com.domain.webdev.domain.com/en/the-game/another-one/another-one/
  • http://www.somesite.com.domain.webdev.domain.com/en/the-game/another-one/
+0

您使用哪種語言? – 2010-04-22 20:04:37

+0

想在這裏使用http://www.movabletype.org/documentation/appendices/modifiers/regex-replace.html – FarazShuja 2010-04-22 20:22:57

回答

1
var myregexp = /^(?:[^\/]*\/){4}([^\/]+)/; 
var match = myregexp.exec(subject); 
if (match != null) { 
    result = match[1]; 
} else { 
    result = ""; 
} 

比賽第四和第五斜線並將結果存儲在變量中result

+0

可愛......我在想,但我沒有把它寫成答案 – dlamotte 2010-04-22 22:23:03

+0

從左邊我只是尋找第四和第五斜線(/)之間的任何文字。 – FarazShuja 2010-04-23 05:25:49

+0

啊,你在更新中擊敗我!驚人的多少有點澄清的要求:) – BenV 2010-04-23 14:22:32

1

URL的哪些部分可能會有所不同,哪些部分是固定的?以下正則表達式將總是與示例中的「/ en /」 - the-game後面的斜線匹配。

(?<=/en/).*?(?=/) 

這一個將匹配第二組包含「Webdev的」任何URL的斜線的內容,假設第一組斜線包含2或3字符的語言代碼。

(?<=.*?webdev.*?/.{2,3}/).*?(?=/) 

希望你可以調整這些例子來完成你正在尋找的東西。

+0

從左側讀我只是尋找第4和第5斜線(/)之間的任何文本。 – FarazShuja 2010-04-23 05:24:44

0

你可能應該使用某種URL解析庫,而不是訴諸使用正則表達式。

在蟒蛇:

from urlparse import urlparse 
url = urlparse('http://www.somesite.com.domain.webdev.domain.com/en/the-game/another-one/another-one/another-one/') 
print url.path 

這將產生:

/en/the-game/another-one/another-one/another-one/ 

從那裏,你可以做簡單的事情,就像從路徑的開始剝離/en/。否則,你一定會犯一個正則表達式錯誤的東西。不要重新發明輪子!