Page 1 of 2

Extract URL from a string

Posted: 06 Sep 2019 07:05
by tsolignani
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

Re: Extract URL from a string

Posted: 06 Sep 2019 11:03
by Desmanto
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.

Re: Extract URL from a string

Posted: 06 Sep 2019 16:23
by anuraag

Code: Select all

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

Re: Extract URL from a string

Posted: 05 Jan 2020 16:43
by tsolignani
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

Re: Extract URL from a string

Posted: 05 Jan 2020 18:47
by Desmanto
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.

Re: Extract URL from a string

Posted: 05 Jan 2020 20:51
by tsolignani
It does work!

Thank you to the both of you.

Wonderful.

Re: Extract URL from a string

Posted: 05 Jan 2020 23:51
by anuraag
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

Re: Extract URL from a string

Posted: 06 Jan 2020 17:56
by Desmanto
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.

Re: Extract URL from a string

Posted: 12 Jan 2020 20:54
by Micky Micky
That's a nice bit of java. I have a specific task to apply it to.

Many thanks to you all.

Re: Extract URL from a string

Posted: 19 Jan 2020 18:47
by Desmanto
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.