如何从Java中的字符串中提取url?

时间:2021-09-05 21:20:02

I have a string containing a short-code which looks like the one below:

我有一个包含短代码的字符串,如下所示:

some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...

I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).

我希望能够首先检查字符串是否包含该短代码,然后再从Java中提取URL(特别是Android)。

5 个解决方案

#1


1  

try as:

试着:

   String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
    if (str.contains("url=\""))
    {
        int indexoff = str.indexOf("url=\"");
        int indexofff = str.indexOf("\"]");
        String strurl = str.substring(indexoff, indexofff - indexoff);
        strurl = strurl.Replace("url=\"", ""); //get url string here

    }

#2


2  

use this regex for checking and grabbing url:

使用此正则表达式来检查和获取URL:

\[\w+\s+url="(?<urllink>)[^"]*"\s*]

and get gorup named urllink

并获得名为urllink的gorup

#3


0  

Android provides several function for this purpose. SOme of this are:

Android为此提供了多种功能。这个的原因是:

#4


0  

String url = "whatever"
Boolean myBool = url.contains("ate");

String contains. Not sure what extract url means, but the string class has lots of useful functions.

字符串包含。不确定提取url的含义,但字符串类有很多有用的功能。

#5


0  

A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

它是Java URL.class的一种强大且可维护的方式,然后,您可以与Regex混合使用

#1


1  

try as:

试着:

   String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
    if (str.contains("url=\""))
    {
        int indexoff = str.indexOf("url=\"");
        int indexofff = str.indexOf("\"]");
        String strurl = str.substring(indexoff, indexofff - indexoff);
        strurl = strurl.Replace("url=\"", ""); //get url string here

    }

#2


2  

use this regex for checking and grabbing url:

使用此正则表达式来检查和获取URL:

\[\w+\s+url="(?<urllink>)[^"]*"\s*]

and get gorup named urllink

并获得名为urllink的gorup

#3


0  

Android provides several function for this purpose. SOme of this are:

Android为此提供了多种功能。这个的原因是:

#4


0  

String url = "whatever"
Boolean myBool = url.contains("ate");

String contains. Not sure what extract url means, but the string class has lots of useful functions.

字符串包含。不确定提取url的含义,但字符串类有很多有用的功能。

#5


0  

A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

它是Java URL.class的一种强大且可维护的方式,然后,您可以与Regex混合使用