2017-05-26 103 views
1

什麼是替換路徑URL佔位符的最佳方式。我有一個需要以下被替換替換路徑URL佔位符格式

/user/:name/password/:password 

/user/{name}/password/{password} 

是否有可能在Java中做到這一點對我來說是圖書館嗎?

+0

我們用此正則表達式'的回調(<= /?):([^ /] *)',然後在回調,作爲密鑰對散列使用1組,然後更換與它的值。 – sln

回答

1

由於格式是如此簡單,:是不是有效的URL字符,我只想用一個基本的正則表達式匹配:其次是任何文字,捕捉如需轉載字。

"/user/:name/password/:password".replaceAll(":(\\w+)","{$1}") 
1

你嘗試使用replaceAll這樣的:

String str = "/user/:name/password/:password"; 
String result = str.replaceAll(":(\\w+)", "{$1}"); 

輸出

/user/{name}/password/{password} 
2

只需使用String#replaceAll可以實現你的方式。

"/user/:name/password/:password".replaceAll(":(\\w+)","{$1}")