2013-05-10 31 views
0

我需要選擇一小部分字符串。選擇此字符串的某個部分

下面是一個例子字符串:http://itunes.apple.com/app/eyelashes/id564783832?uo=5

我需要:564783832

幾件事情要記住:

  • 數量將始終id前面(即id564783832
  • 可能有或可能沒有?uo=5後面的數字(也可能是除之外的其他參數)
  • 我需要可以是不同的長度的字符串(不總是9位數字)
  • 前述id將具有類似的格式(同#斜線的,但文本的文本將是不同的)

這將最終用Ruby來實現。

+0

可能還有其他的東西在ID前面? – 2013-05-10 16:32:57

+2

這是一個:'(tool)?|(language)?|(yourcodes)?' – Kent 2013-05-10 16:33:33

+0

@Kent:Sorry,post updated。我正在使用Ruby。 – Shpigford 2013-05-10 16:36:18

回答

2

不知道你的語言/工具,只是假設看後面被支持。

'(?<=id)\d+' 
+0

你也許想檢查結束,這可能是可能的 - 'itunes.apple.com/app/abcid1234/id12498761' – Dukeling 2013-05-10 16:37:05

+0

然後我假設OP想要兩個數字。 (基於Keep記住列表):)這兩個數字(後面的id)都與該列表中的條件相匹配。 – Kent 2013-05-10 16:41:25

+0

**我**會猜測OP只想要最後一個,但這只是我的看法。 – Dukeling 2013-05-10 16:43:20

0

可以比擬的通過「ID」開頭的數字有些序列 - 這是假定這些都是由「ID」開頭的數字的唯一序列:

(?<=id)\d++ 

測試用例在Java中:

public static void main(String[] args) { 
    String input = "http://itunes.apple.com/app/eyelashes/id564783832?uo=5"; 
    Pattern pattern = Pattern.compile("(?<=id)\\d++"); 
    Matcher matcher = pattern.matcher(input); 
    while (matcher.find()) { 
     System.out.println(matcher.group()); 
    } 
} 

輸出

564783832 
0

這裏是我的:

[\w\/]+id(\d+)(\?|$)