可能重複:
How to extract parameters from a given urljava的正則表達式URL解析
我想從這個URL參數只檢索的數字:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我已經試過這個沒有運氣:
^.*communityUuid=
任何幫助將是很好的。
可能重複:
How to extract parameters from a given urljava的正則表達式URL解析
我想從這個URL參數只檢索的數字:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
我已經試過這個沒有運氣:
^.*communityUuid=
任何幫助將是很好的。
我建議不要簡單的字符串操作路線。它更冗長,更容易出錯。你可能也得到了內置類一點點幫助,然後用你的,你用URL(以「&」分隔參數)工作知識來指導你實現:
String queryString = new URL("http://tesing12/testds/fdsa?communityUuid=45352-32452-52").getQuery();
String[] params = queryString.split("&");
String communityUuid = null;
for (String param : params) {
if (param.startsWith("communityUuid=")) {
communityUuid = param.substring(param.indexOf('=') + 1);
}
}
if (communityUuid != null) {
// do what you gotta do
}
這給了你檢查URL格式良好的好處,並避免可能由類似命名參數引起的問題(字符串操作路由將報告「abc_communityUuid」以及「communityUuid」)的值。
此代碼的一個有用的擴展是在遍歷「params」時構建地圖,然後查詢地圖以獲取所需的任何參數名稱。
我看不出有任何理由使用正則表達式。
我只是這樣做:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int index = url.indexOf(token) + token.length();
String theNumbers = url.substring(index);
注:
您可能必須尋找下一個參數,以及:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int startIndex = url.indexOf(token) + token.length();
// here's where you might want to use a regex
String theNumbers = url.substring(startIndex).replaceAll("&.*$", "");
在上面的示例代碼中,不會索引字符串communityUuid中的「c」的索引值嗎?字符串「theNumbers」因此將是communityUuid = 45352-32452-52,這不是OP所要求的。 int變量需要適當增加。建議你更新你的答案,因爲它的概念是正確的!然後,我可以放棄它。 –
太棒了,感謝您的幫助=)。 – user1103205
@AuuragKapur - 好點...即將編輯。 – jahroy
爲什麼不直接搜索'communityUuid ='並在該索引後面抓取所有內容。我沒有看到你想要使用正則表達式。 – jahroy
之後的網址會有更多的數據嗎? –
,因爲我不擔保它是字符串中的最後一項,或許更好的示例是htt:// tesing12/testds/fdsa?communityUuid = 45352-32452-52?topic = 890531-532-532 – user1103205