user3776241
user3776241

Reputation: 543

Dynamically replace part in URL using Regex

I tried searching for something similar, and couldn't find anything. I'm having difficulty trying to replace a few characters after a specific part in a URL.

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

I want to remove the /v/ part, leave the t1.0-9, and also remove the /s130x130/.I cannot just replace s130x130, because those may be different variables. How do I go about doing that?

I have a previous URL where I am using this code:

            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=", "");
                }
            }

Can I do something similar with the above URL?

Upvotes: 0

Views: 1090

Answers (1)

OnlineCop
OnlineCop

Reputation: 4069

With the regex

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

replaced with

/

It turns the string from

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

to

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

as seen here. Is this what you're trying to do?

Upvotes: 1

Related Questions