Android Question Get url from String

ilan

Expert
Licensed User
Longtime User
i remember @Erel wrote a very useful snippet where you can get a link from a string (Text)

for example:

B4X:
dim str as string = "Yesterday i came across a very useful website www.b4x.com and i wanted to share it with you."

now i want to get the link (if such exist) from that text but i cant find that snippet from erel :(
 

udg

Expert
Licensed User
Longtime User
I tried the following with multiple URLs in the same string
B4X:
\w*\.\w*\.*\w*[^\s]+|https?://\w*\.\w*\.*\w*[^\s]+
Tried with phrase: "Yesterday i came across a very useful website www.b4x.com and i wanted to share it with you. have a look to http://www.hello.com and https://hello.us too."

Note: added [^\s]+ after reading @sorex post to take in account subdirs.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
i dont know how i did it but this seem to work :confused:

B4X:
Sub manfred
    Dim str As String = "Yesterday i https://www.facebook.com/profile.php?id=100011525702168 came across a very useful website www.b4x.com and i wanted to share it with you."
 
    Dim m As Matcher = Regex.Matcher("(((https?|http|ftp)://)?[a-z0-9-.]*\.[a-z0-9-./]*\.[a-z0-9-.=?]*)", str)
   
    Do While m.Find
        Log(m.Group(1))
    Loop
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi ... this works for me:

B4X:
    Dim StringContainingLink As String = "Yesterday i came across a very useful website www.b4x.com and http://www.google.com i wanted to share it with you also you should have a look at https://google.com:5123 to see what is going on."
    Dim mPattern As String = "(?:^|[\W])((ht|f)tp(s?):\/\/|www\.)" & "(([\w\-]+\.){1,}?([\w\-.~]+\/?)*" & "[\p{Alnum}.,%_=?&#\-+()\[\]\*$~@!:/{};']*)"
    Dim mMatcher As Matcher = Regex.Matcher2(mPattern, Bit.Or(Regex.CASE_INSENSITIVE, Regex.MULTILINE), StringContainingLink)
    Do While mMatcher.Find
        Dim matchStart As Int = mMatcher.GetStart(1)
        Dim matchEnd As Int = mMatcher.GetEnd(0)
        Log($"Start: ${matchStart} End: ${matchEnd} String: ${StringContainingLink.SubString2(matchStart, matchEnd)}"$)
    Loop

PERFECT !!! thank you very much! :)
i am also able to get a facebook link!
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi ... this works for me:

B4X:
    Dim StringContainingLink As String = "Yesterday i came across a very useful website www.b4x.com and http://www.google.com i wanted to share it with you also you should have a look at https://google.com:5123 to see what is going on."
    Dim mPattern As String = "(?:^|[\W])((ht|f)tp(s?):\/\/|www\.)" & "(([\w\-]+\.){1,}?([\w\-.~]+\/?)*" & "[\p{Alnum}.,%_=?&#\-+()\[\]\*$~@!:/{};']*)"
    Dim mMatcher As Matcher = Regex.Matcher2(mPattern, Bit.Or(Regex.CASE_INSENSITIVE, Regex.MULTILINE), StringContainingLink)
    Do While mMatcher.Find
        Dim matchStart As Int = mMatcher.GetStart(1)
        Dim matchEnd As Int = mMatcher.GetEnd(0)
        Log($"Start: ${matchStart} End: ${matchEnd} String: ${StringContainingLink.SubString2(matchStart, matchEnd)}"$)
    Loop

you should add this snippet to the Snippet Forum!

the reason i need to get a link is this. i have implemented firebase notification in my apps now i want to send a notification to my users and if they click on the notification
the app will search if that notification contains a link and instead of opening the app it will open the link. like this i can send a notification that will include a link to a new app and user will be able to open it by clicking on the notification. until now i solved it in a different way but i wanted to have a more dynamic solution.

thanks to all of you guys :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Strange, works here (tried on online regex) . (I've missed the "" from the regex pattern, have you included?)

yes sure i have add "" but i didnot get any result. i tried it on b4j.

but @mindful solution is the right solution. i am able to get also links that end with numbers (like facebook links) or any other link.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Full Source Example (B4J)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show

    Dim StringContainingLink As String = "Yesterday i came across a very useful website https://www.facebook.com/profile.php?id=100011525702168 and i wanted to share it with you, btw you should also check this one www.b4x.com and also http://www.sagital.net"
    mindful(StringContainingLink)
End Sub

Sub mindful(url As String)
    Dim mPattern As String = "(?:^|[\W])((ht|f)tp(s?):\/\/|www\.)" & "(([\w\-]+\.){1,}?([\w\-.~]+\/?)*" & "[\p{Alnum}.,%_=?&#\-+()\[\]\*$~@!:/{};']*)"
    Dim mMatcher As Matcher = Regex.Matcher2(mPattern, Bit.Or(Regex.CASE_INSENSITIVE, Regex.MULTILINE), url)
    Do While mMatcher.Find
        Dim matchStart As Int = mMatcher.GetStart(1)
        Dim matchEnd As Int = mMatcher.GetEnd(0)
        Log(url.SubString2(matchStart, matchEnd))
    Loop
End Sub

Logs:

 
Upvote 0
Top