2014-09-20 30 views
0

我需要一個正則表達式( 「=」 和 「&」,例如 「= 376629 &」 之間實際的ID(數字))是這樣的:正則表達式中的URL相匹配的ID

http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=376629&part=Borrowing+100%2c000+Arrows

我發現是這樣的:

 [^0-9$,] 

但這將不會匹配這樣的:

http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=376629&part=Borrowing+100%2c000+Arrows

只有這個:

http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=85808&part=Boseiju%2c+Who+Shelters+All

本場比賽將是我想要的 「85808」。

可能是你可以幫助我,謝謝。

+0

'=','&'也再使用,'= \ d +' – 2014-09-20 04:57:59

回答

1

正則表達式

id=(\d+) 

第一次捕捉包含結果

測試串

http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=376629&part=Borrowing+100%2c000+Arrows 
http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=85808&part=Boseiju%2c+Who+Shelters+All 

結果

  • MATCH 1
    1. 376629
  • MATCH 2
    1. 85808

演示

Online Demo

C#樣品

string hrefValue = "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=85808&part=Boseiju%2c+Who+Shelters+All"; 
    string id = Regex.Match(hrefValue, @"id=(\d+)").Groups[1].Value; 

Online C# Demo

如果你想
+0

ty,它的工作:) – user254197 2014-09-20 05:26:58

0

嘗試這種模式:

(?<==)[0-9]+(?=&) 

Explanation

Demo

+0

對不起,我仍然獲得了出現FormatException,當excecute您正則表達式(Regex.Replace(hrefValue,@ 「?(?<==)[0-9] +(?=&)」,string.Empty))... – user254197 2014-09-20 04:53:33

+0

我猜不支持後面的不支持 – pushpraj 2014-09-20 04:54:13

+0

@ user254197你爲什麼要用replace方法? – 2014-09-20 04:56:03