Extract URL from a string

Post your questions and help other users.

Moderator: Martin

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

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.

Tiziano

User avatar
Desmanto
Posts: 2709
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), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

anuraag
Posts: 371
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

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Extract URL from a string

Post by tsolignani » 05 Jan 2020 16:43

Thank you.

Please, forgive me it took a long time to follow up, I made some tries but with no joy at the end.

An example of string containing an URL I would like to spot and assign to a variable:
«La #mindfulness è uno degli strumenti più importanti e che raccomando più spesso ai miei clienti del counseling, perché consente di centrarsi, scendere di un piano, dalla testa al cuore, riconnettersi con le nostre parti più profonde, combattere quindi la #mentalizzazione e offre un sistema molto efficace per affrontare le difficoltà.»

#counseling #meditazione #consapevolezza #cuore #partiprofonde

http://blog.solignani.it/2019/05/07/la- ... pevolezza/

clicca il link per approfondire...
Attached is a flow I tried to make but it gives an error. Sometimes it does not give an error but it does not extract the url all the same.

@anuraag: thank you, but how am I supposed to use Java inside automagic?

Thank you all
Attachments
flow_SplitUrlAndText_20200105_174150.xml
(2.34 KiB) Downloaded 787 times

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract URL from a string

Post by Desmanto » 05 Jan 2020 18:47

Thanks to anuraag, I don't need to define a very long regex again. Java has provide some important regex pattern.

To use the java, just use the script provided by anuraag above. That line will give the regex pattern for the URL. It is much better than mine above.
I have updated it to use that regex pattern instead, just use this in the script after your input dialog.

Code: Select all

find = findAll(value, getJavaStaticField("android.util.Patterns", "WEB_URL")); 
url = find[0];
You should get the correct url from it.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Extract URL from a string

Post by tsolignani » 05 Jan 2020 20:51

It does work!

Thank you to the both of you.

Wonderful.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Extract URL from a string

Post by anuraag » 05 Jan 2020 23:51

I come to know about this while creating "open link with" flow. There are many more regex available like phone number, email, ip address.
https://developer.android.com/reference ... terns.html

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract URL from a string

Post by Desmanto » 06 Jan 2020 17:56

anuraag wrote:
05 Jan 2020 23:51
I come to know about this while creating "open link with" flow. There are many more regex available like phone number, email, ip address.
https://developer.android.com/reference ... terns.html
Thank you again for the insight. I just recreate my QRCode Scanner flow and I use this to extract the URL. Now I am trying to separate the text processing part and use all possible regex to extract out various information. A kind of text extracting flow, where you pass the text and extract out the url, phone number, ip and email. Later we can use various activity to link to it, example visit the url, call/sms phone number, ping ip or email the address.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: Extract URL from a string

Post by Micky Micky » 12 Jan 2020 20:54

That's a nice bit of java. I have a specific task to apply it to.

Many thanks to you all.
Crude but it works.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract URL from a string

Post by Desmanto » 19 Jan 2020 18:47

My flows have finished. Now it is testing time for several days before I share it. It contains a QRCode scanner flow and another Text Extractor flow which is multi purpose and can be called from other flow.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply