使用 Java 中的 Regex 确定字符串是否为 URL [英] Determine if string is a URL with Regex in Java

查看:23
本文介绍了使用 Java 中的 Regex 确定字符串是否为 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么是检查字符串是否为有效 URL 的最佳正则表达式
Java-如何检测URL的存在一个字符串.

有没有办法查看一个字符串是否是一个有效的 URL?我正在使用 MIDP 库,但 MIDP 中没有正则表达式.非常感谢任何帮助.

Is there a way to see if a string is a valid URL? I'm using MIDP library and there are not regular expressions in MIDP. Any help is greatly appreciated.

推荐答案

为什么不直接从字符串中构造一个 java.net.URL 并捕获可能抛出的任何异常?

Why not just construct a java.net.URL out of the string and catch any exceptions that might be thrown?

String something;
try {
  URL url = new URL(something);
} catch (MalformedURLException e) {
  // it wasn't a URL
}

这篇关于使用 Java 中的 Regex 确定字符串是否为 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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