summers summers - 25 days ago 6x
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.


Chrome: ""

Twitter: "Guys check out this link it's so cool"

So in case of twitter I would like to get rid of all the context and have only the link remaining,ie,

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

Any regex to sort this out?

protected void onCreate(Bundle savedInstanceState)

// 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

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


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

        String urlStr =;

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


        return links;