2014-11-14 72 views
0

我試着搜索類似的東西,但找不到任何東西。我很難在URL中的特定部分之後替換幾個字符。使用正則表達式動態替換URL中的部分

這裏是網址:https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B

我想刪除/ V /一部分,離開t1.0-9,並且還去掉/s130x130/.I不能只需更換s130x130,因爲這些可能是不同的變量。我該如何去做呢?

我以前的網址,讓我使用這個代碼:

  if (pictureUri.indexOf("&url=") != -1) 
      { 
       String replacement = ""; 

       String url = pictureUri.replaceAll("&", "/"); 

       String result = url.replaceAll("().*?(/url=)", 
         "$1" + replacement + "$2");     

       String pictureUrl = null; 

       if (result.startsWith("/url=")) 
       { 
        pictureUrl = result.replace("/url=", ""); 
       } 
      } 

我可以做上面的網址相似的地方?

+0

'()。*?'應該做什麼? – OnlineCop 2014-11-14 23:04:54

回答

1

隨着正則表達式

/v/|/s\d+x\d+/ 

/ 

替換原來從

https://scontent-b.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/s130x130/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B 

https://scontent-b.xx.fbcdn.net/hphotos-xpf1/t1.0-9/10390064_10152552351881633_355852593677844144_n.jpg?oh=479fa99a88adea07f6660e1c23724e42&oe=5519DE4B 
字符串

如所見here。這是你想要做的嗎?