metalink
metalink

Reputation: 584

get substring from string Java(Android)

Could you help me with this problem? I have String like this:

<p class="youtube_sc mobile "><a href="vnd.youtube:bMHJODdp7-U" title="YouTube video player"><img src="http://i.ytimg.com/vi/bMHJODdp7-U/hqdefault.jpg" /><span class="play-button-outer" title="Click to play video"><span class="play-button"></span></span></a></p>

And I need to get only what will be after "youtube:" in this example this part of line: bMHJODdp7-U
I use this code of my example:

String path = strin.replaceAll(".*(youtube:\\S+)", "$ title");

Where strin is the String line. But this code only replace what I need to " title"
Any ideas?

Upvotes: 2

Views: 481

Answers (3)

zawhtut
zawhtut

Reputation: 8541

Change your regular expression to

(?:youtube:)(\S+)(?:")

Upvotes: 0

anubhava
anubhava

Reputation: 784938

You can use this regex for search:

^.*?youtube:([^"]+).*$

and replace it by:

$1

Code:

String path = strin.replaceFirst("^.*?youtube:([^\"]+).*$", "$1");
//=> bMHJODdp7-U

RegEx Demo

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26067

Explanation - youtube:(.*?)\" matches for text in between youtube: and "

  public static void main(String[] args) {
        String s = "<p class=\"youtube_sc mobile \"><a href=\"vnd.youtube:bMHJODdp7-U\" title=\"YouTube video player\"><img src=\"http://i.ytimg.com/vi/bMHJODdp7-U/hqdefault.jpg\" /><span class=\"play-button-outer\" title=\"Click to play video\"><span class=\"play-button\"></span></span></a></p>";
        Pattern pattern = Pattern.compile("youtube:(.*?)\"");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println(matcher.group(1));
        }
    }

Output

bMHJODdp7-U

Upvotes: 1

Related Questions