1

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);
    }
}
2
  • You could search a string that contains at least one "." and maybe 4 characters? (Without space) Commented Sep 13, 2016 at 17:45
  • how is this code relevant / what have you tried so far? Commented Sep 13, 2016 at 18:03

3 Answers 3

6

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.