Extract URL from a string

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Posts: 54
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy

Extract URL from a string

Post by tsolignani » 06 Sep 2019 07:05

Good morning everyone

I have a string which contains both some text and an URL, the latter starting with http ecc.

Did nyone write something to extract the URL part maybe?

What I would like to do is just split the two parts and get two new strings, a former with the text only and a second one with just the hyperlink.

Thank you.

Have a nice day and weekend.


User avatar
Posts: 2036
Joined: 21 Jul 2017 17:50

Re: Extract URL from a string

Post by Desmanto » 06 Sep 2019 11:03

What is your text example? Is the domain always the same? And where is the position of the text outside of the url?

I assume your domain is random, separated by space with the text you want to take. Here is a very loose regex to capture your group.

Code: Select all

input = "https://www.url.com hello world";

find = findAll(input, "((?:https?://)?(?:www)?.*\\..*?) (.*)", true);
url = find[0][1];
text = find[0][2];
?: inside () is to exclude it from capture group number
? infront of char is to make it optional. s? hence means it can be 'with s' or 'without s'
(?:wwww)? hence is to exclude www from capture group, and make the whole www optional (can have www or don't have)
.*\\..*? is to match any number of char (.*), followed by a dot (\\.), then any number of char (.*). The ? infront of * is to tell the matching of .* to be non greedy, matching as few as possible. I know the ? is used in to many ways, but it really useful in most cases.
(.*) last part is to match the hello world, or any additional text after the url.

Captured find, will have group 1 and 2. Group 1 is the url and group 2 is text.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

Posts: 273
Joined: 24 Jan 2015 02:06

Re: Extract URL from a string

Post by anuraag » 06 Sep 2019 16:23

Code: Select all

getJavaStaticField("android.util.Patterns", "WEB_URL")
Above code can be used for url regex

Post Reply