B4A Library Url Detector - detect and extract urls in a long piece of text

This code is based on https://github.com/linkedin/URL-Detector
First you need download 2 jar:

https://repo1.maven.org/maven2/com/linkedin/urls/url-detector/0.1.17/url-detector-0.1.17.jar
https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar

And copy to your add-library folder
And here code / function to extract list url from any text string

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#AdditionalJar:url-detector-0.1.17.jar
#AdditionalJar:commons-lang3-3.1.jar
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim str_input="Just because the.com heheyahoo.com weather is starting to get warm, does not mean that you should look sloppy. Get inspired and check out our collection of men's summer outfits.    famousoutfits    " As String
    Private NativeMe As JavaObject
    NativeMe.InitializeContext
    Dim s As List = NativeMe.RunMethod("url_detect", Array As String(str_input))
    Log(s.Size)

For i=0 To s.Size-1
    Log(s.Get(i))
Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
#IF JAVA
import com.linkedin.urls.Url;
import com.linkedin.urls.detection.UrlDetector;
import com.linkedin.urls.detection.UrlDetectorOptions;
import java.util.List;
import java.util.ArrayList;
public  List<String> url_detect(String stringinput){
        UrlDetector parser = new UrlDetector(stringinput, UrlDetectorOptions.Default);
        List<Url> found = parser.detect();
        List<String> itemsToAdd = new ArrayList<String>();
        for(Url url : found) {
            itemsToAdd.add(url.getFullUrl());
        }
        return itemsToAdd;
    }
#End If

library can find and detect any urls such as:



 
Last edited:

tuhatinhvn

Active Member
Licensed User
Longtime User
Thank for your suggestion, library can find and detect any urls such as, so this exam string is too small to detect

 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…