2

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?

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this regex for search:

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

and replace it by:

$1

Code:

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

RegEx Demo

Comments

0

Change your regular expression to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.