B4J Code Snippet Get URL from String using RegEx

B4X:
Public Sub GetLinkFromString(StringContainingLink 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), 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
End Sub

You should run it like this:
B4X:
Dim str 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. Please vote https://www.nowheresite.com/profile.php?id=1213124325435465412312hasvbdhasvhd for me"
GetLinkFromString(str)

and you will get:
B4X:
Waiting for debugger to connect...
Program started.
Start: 46 End: 57 String: www.b4x.com
Start: 62 End: 83 String: http://www.google.com
Start: 145 End: 168 String: https://google.com:5123
Start: 206 End: 283 String: https://www.nowheresite.com/profile.php?id=1213124325435465412312hasvbdhasvhd
 
Top