summers summers - 2 months ago 9
Java Question

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);
}
}

Answer

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;
}