Zen
Zen

Reputation: 2728

Android: extract link from String value

I would like to have link from the share intent. When I receive a link via chrome its properly formatted, but sometimes other apps add text too.

Example:

Chrome: "www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play"

Twitter: "Guys check out this link it's so cool https://www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play"

So in case of twitter I would like to get rid of all the context and have only the link remaining,ie, www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play

Note: Link may be of any format https://.. (or) www. .. (or) recode.net/... (without the www at the beginning).

Any regex to sort this out?

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shareintent);

    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) 
    {
        if ("text/plain".equals(type)) 
        {
            // Handle text being sent
            handleSendText(intent); 
        }
    }
}

void handleSendText(Intent intent)
{
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) 
    {
        // Update UI to reflect text being shared
        TextView tvShare = (TextView) findViewById(R.id.tvShare);
        tvShare.setText(sharedText);
    }
}

Upvotes: 1

Views: 3360

Answers (3)

RAINA
RAINA

Reputation: 1012

You can just extract the Url from a string using the Matcher Class.following code will extract all the links included in the string to an array of Urls.

 Matcher webMatcher = Patterns.WEB_URL.matcher(stringWhichContainsUrl);
        ArrayList<String> hyperLinks = new ArrayList<>();

        while (webMatcher.find()) {
            String res = webMatcher.group();
            hyperLinks.add(res);
        }

        Log.e("links", hyperLinks.toString());

Upvotes: 0

Zen
Zen

Reputation: 2728

The following method does the trick:

//Pull all links from the body for easy retrieval
public ArrayList<String> pullLinks(String text) 
{
    ArrayList<String> links = new ArrayList<String>();

    //String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
    String regex = "\\(?\\b(https?://|www[.]|ftp://)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);

    while(m.find()) 
    {
        String urlStr = m.group();

        if (urlStr.startsWith("(") && urlStr.endsWith(")"))
        {
            urlStr = urlStr.substring(1, urlStr.length() - 1);
        }

            links.add(urlStr);
    }

        return links;
}

Upvotes: 6

Aman Grover
Aman Grover

Reputation: 1641

You could recognize and extract a particular pattern from the String.

// Pattern for recognizing a URL, based off RFC 3986 
private static final Pattern urlPattern = Pattern.compile(
        "(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)" 
                + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*" 
                + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)", 
        Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

Example :

Matcher matcher = urlPattern.matcher("foo bar http://example.com baz");
while (matcher.find()) {
    int matchStart = matcher.start(1);
    int matchEnd = matcher.end();
    // now you have the offsets of a URL match 
} 

reference: may be other answers there will be useful to you as well

Upvotes: 0

Related Questions