Extract URL from a string
Moderator: Martin
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Extract URL from a string
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
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
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.
?: 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.
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];
? 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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Extract URL from a string
Code: Select all
getJavaStaticField("android.util.Patterns", "WEB_URL")
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract URL from a string
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:
@anuraag: thank you, but how am I supposed to use Java inside automagic?
Thank you all
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:
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.«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...
@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 803 times
Re: Extract URL from a string
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.
You should get the correct url from it.
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];
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract URL from a string
It does work!
Thank you to the both of you.
Wonderful.
Thank you to the both of you.
Wonderful.
Re: Extract URL from a string
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
https://developer.android.com/reference ... terns.html
Re: Extract URL from a string
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.anuraag wrote: ↑05 Jan 2020 23:51I 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
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
-
- Posts: 179
- Joined: 16 Oct 2019 17:38
Re: Extract URL from a string
That's a nice bit of java. I have a specific task to apply it to.
Many thanks to you all.
Many thanks to you all.
Crude but it works.
Re: Extract URL from a string
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.