【问题标题】:Android: extract link from String valueAndroid:从字符串值中提取链接
【发布时间】:2017-01-21 09:40:20
【问题描述】:

我想获得分享意图的链接。当我通过 chrome 收到链接时,其格式正确,但有时其他应用程序也会添加文本。

例子:

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

Twitter:“大家看看这个链接,太酷了https://www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play”

因此,对于 twitter,我想摆脱所有上下文,只保留链接,即 www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play

注意:链接可以是任何格式 https://..(或)www。 ..(或)recode.net/...(开头没有 www)。

任何正则表达式来解决这个问题?

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

【问题讨论】:

  • 您可以搜索至少包含一个“.”的字符串。也许是 4 个字符? (没有空格)
  • 这段代码的相关性如何/到目前为止您尝试了什么?

标签: java android regex


【解决方案1】:

以下方法可以解决问题:

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

【讨论】:

    【解决方案2】:

    您可以识别并从字符串中提取特定模式。

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

    例子:

    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 
    } 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多