简体   繁体   中英

How to extract a url from a string in Java?

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).

use this regex for checking and grabbing url:

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

and get gorup named urllink

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

    }
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM