如何检测字符串中是否存在 URL [英] How to detect the presence of URL in a string

查看:42
本文介绍了如何检测字符串中是否存在 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字符串 请访问 http://stackoverflow.com.检测到字符串的 url 部分,许多浏览器/IDE/应用​​程序会自动添加一个锚.所以它变成了请去http://stackoverflow.com.

I have an input String say Please go to http://stackoverflow.com. The url part of the String is detected and an anchor <a href=""></a> is automatically added by many browser/IDE/applications. So it becomes Please go to <a href='http://stackoverflow.com'>http://stackoverflow.com</a>.

我需要使用 Java 做同样的事情.

I need to do the same using Java.

推荐答案

为此使用 java.net.URL!

嘿,为什么不为这个java.net.URL"使用java中的核心类并让它验证URL.

Use java.net.URL for that!!

Hey, why don't use the core class in java for this "java.net.URL" and let it validate the URL.

虽然以下代码违反了仅在异常情况下使用异常"的黄金原则,但对我来说,尝试为 Java 平台上非常成熟的东西重新发明轮子是没有意义的.

While the following code violates the golden principle "Use exception for exceptional conditions only" it does not make sense to me to try to reinvent the wheel for something that is veeery mature on the java platform.

代码如下:

import java.net.URL;
import java.net.MalformedURLException;

// Replaces URLs with html hrefs codes
public class URLInString {
    public static void main(String[] args) {
        String s = args[0];
        // separate input by spaces ( URLs don't have spaces )
        String [] parts = s.split("\s+");

        // Attempt to convert each item into an URL.   
        for( String item : parts ) try {
            URL url = new URL(item);
            // If possible then replace with anchor...
            System.out.print("<a href="" + url + "">"+ url + "</a> " );    
        } catch (MalformedURLException e) {
            // If there was an URL that was not it!...
            System.out.print( item + " " );
        }

        System.out.println();
    }
}

使用以下输入:

"Please go to http://stackoverflow.com and then mailto:oscarreyes@wordpress.com to download a file from    ftp://user:pass@someserver/someFile.txt"

产生以下输出:

Please go to <a href="http://stackoverflow.com">http://stackoverflow.com</a> and then <a href="mailto:oscarreyes@wordpress.com">mailto:oscarreyes@wordpress.com</a> to download a file from    <a href="ftp://user:pass@someserver/someFile.txt">ftp://user:pass@someserver/someFile.txt</a>

当然可以用不同的方式处理不同的协议.您可以使用 URL 类的 getter 获取所有信息,例如

Of course different protocols could be handled in different ways. You can get all the info with the getters of URL class, for instance

 url.getProtocol();

或者其他属性:规范、端口、文件、查询、引用等

Or the rest of the attributes: spec, port, file, query, ref etc. etc

http://java.sun.com/javase/6/docs/api/java/net/URL.html

处理所有协议(至少是所有 java 平台知道的协议),并且作为一个额外的好处,如果有任何 java 当前无法识别的 URL 并最终被合并到 URL 类中(通过库更新),您会透明地得到它!

Handles all the protocols ( at least all of those the java platform is aware ) and as an extra benefit, if there is any URL that java currently does not recognize and eventually gets incorporated into the URL class ( by library updating ) you'll get it transparently!

这篇关于如何检测字符串中是否存在 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆