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 :(
 

DonManfred

Expert
Licensed User
Longtime User
it will not work as the code is getting the url from the src tag of an image.
You need to rewrite the regex to get the partially (it is NOT a URL as it does not have a scheme) url in the string.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
non reg-ex method that works on the format you have there

B4X:
Dim str As String = "Yesterday i came across a very useful website www.b4x.com and i wanted to share it with you."
Dim url As String
url=str.SubString2(str.IndexOf("www."),str.IndexOf2(" ",str.IndexOf("www.")))
Log(url)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Im not good at regex but try this please

B4X:
    Dim str As String = "Yesterday i came across a very useful website www.b4x.com and i wanted to share it with you."
  
    Dim m As Matcher = Regex.Matcher("([a-z0-9-.]*\.[a-z0-9-.]*\.[a-z]*)", str)
    If m.Find Then
        Log(m.Group(1))
    End If

 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i came up with this:


B4X:
'Use:

Sub btn_Click
    Dim str As String = "Yesterday i came across a very useful website https://www.b4x.com and i wanted to share it with you."
 
    For Each txt As String In returnLink(str)
        Log(txt)
    Next
End Sub

Sub returnLink(txt As String) As List
    Dim l As List
    l.Initialize
 
    Dim urlRegex As String = "(https?|ftp|gopher|telnet|file).*(jpg|mp4|com|net|il|org|uk|at|de)"

    Dim matcher As Matcher
    matcher = Regex.Matcher(urlRegex,txt)

    Do While matcher.Find
        l.Add(matcher.Match) 
    Loop 
 
    Return l
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Im not good at regex but try this please

B4X:
    Dim str As String = "Yesterday i came across a very useful website www.b4x.com and i wanted to share it with you."
 
    Dim m As Matcher = Regex.Matcher("([a-z0-9-.]*\.[a-z0-9-.]*\.[a-z]*)", str)
    If m.Find Then
        Log(m.Group(1))
    End If


very good @DonManfred but can you add also the "http://" at start when it contains http:// or ftp:// or https://
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
1. You have a wrong url in the string. must be http://
2. You needed to change the string to get it to work (see my answer #10)

if you put http:// it will work too. the only problem is if i have 2 links it that string then i wont get the right result but with 1 link it works
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
this works for me with 1 link in the string

B4X:
    Dim str As String = "Yesterday i came across a very useful website https://www.b4x.com and i wanted to share it with you."
    Log(returnLink(str))

B4X:
Sub returnLink(txt As String) As String
    Dim urlRegex As String = "(https?|ftp|gopher|telnet|file).*(jpg|mp4|com|net|il|org|uk|at|de)"

    Dim matcher As Matcher
    matcher = Regex.Matcher(urlRegex,txt)

    If matcher.Find Then Return matcher.Group(0) Else Return ""
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Try
B4X:
regexpattern= (?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])
View attachment 52179

sorry but i am not getting any matches

B4X:
    Dim m As Matcher = Regex.Matcher("(((https?|http|ftp)://)?[a-z0-9-.]*\.[a-z0-9-.]*\.[a-z]*)", str)
    If m.Find Then
        Log(m.Group(1))
    End If
?

good, we are getting close :D

now it works great but what happens if the end of the url is not .com ?

like my facebook page: https://www.facebook.com/profile.php?id=100011525702168

maybe we will need to search for a space after the url link starts and like this we could also get the url from such a link?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Upvote 0

mindful

Active Member
Licensed 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
 
Upvote 0
Top