3

I have a text that contains youtube URLs.I want to convert all youtube URLs to embed code. For example :

From

To

* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/S-thTTqefls?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++


* Sample
++++
<iframe width="560" height="315" src="https://www.youtube.com/embed/xcJtL7QggTI?start=60" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
++++

First Method

I want to push the matched youtube URL between the embed code.But I don't know how can I do this?

> "++++\n" + "<iframe width=\"560\" height=\"315\" src=\"" + How can I
> push URL here with following code?
> + "\" frameborder=\"0\" allow=\"autoplayencrypted-media\" allowfullscreen></iframe>\n++++"

String Content = readFileAsString(Path); Pattern MY_PATTERN = Pattern.compile("^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$"); Matcher m = MY_PATTERN.matcher(Content); Content = m.replaceAll("++++\n" + "\n++++");

Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

Second Method

I tried to place embed code by finding all youtube URL in the loop and placing embed code bu substring method.

"\n++++\n<iframe width=\"560\" height=\"315\" src=\"" 44 character

https://youtu.be/S-thTTqefls?t=60 max 34 chracter

for (int i = -1; (i = Content.indexOf("https://youtu.be", i + 1)) != -1; i++) {
    Content = Content.substring(0, i) + "\n++++\n<iframe width=\"560\" height=\"315\" src=\"" + Content.substring(i, Content.length());
    Content = Content.substring(0, i + 78) + "\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>\n++++\n" + Content.substring(i + 78, Content.length());
}
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");

But some https://youtu.be URLs do not get an embed code. But I have a weird output like this.

> ++++ <iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src="<iframe width="560" height="315" src="
> ++++ <iframe width="560" height="315" src=" https://youtube.com/watch?v=xcJtL7QggTI?t=60" frameborder="0"
> allow="autoplay; encrypted-media" allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++
> 
> " frameborder="0" allow="autoplay; encrypted-media"
> allowfullscreen></iframe>
> ++++
my-lord
  • 2,453
  • 3
  • 12
  • 26
  • By text, do you mean text file? – Soutzikevich Aug 04 '18 at 12:44
  • Yes. I read all the contents of the text file and transfer Comment String. – my-lord Aug 04 '18 at 13:09
  • I am not sure why you are using regex here. It looks like you just want to replace line after `* Sample` regardless what it is so no regex is needed, at least based on what you posted. In that case simply loop over all lines, check if it is `* Sample`, if yes set some `boolean` value like `afterSample` to true. If `afterSample` is true read line but write its replaced version (surrounded with ` – Pshemo Aug 04 '18 at 13:23
  • I have different youtube URLs which each other's id is different. ...watch?v=xcJtL7QggTI ...watch?v=thTTqefls Is there any easy way without using regex? – my-lord Aug 04 '18 at 13:34
  • But from what I see you are not modifying those URLs. So what is the point of regex here if you know location of those URLs in string (next line after `* Sample`)? – Pshemo Aug 04 '18 at 13:39
  • Pshemo, oh thanks I didn't notice this,I will write a function that finds all occurrences and additional requirements. – my-lord Aug 04 '18 at 13:56

2 Answers2

1

The problem is the Regex pattern which I didn't expect. I have made search and have tried all regex patterns and have found the following

This regex pattern finds all URLs successfully. https://stackoverflow.com/a/31726735/9134980

Pattern MY_PATTERN = Pattern.compile("((http(s)?:\\/\\/)?)(www\\.)?((youtube\\.com\\/)|(youtu.be\\/))[\\S]+");
Matcher m = MY_PATTERN.matcher(Content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, "++++\n" + "<iframe width=\"560\" height=\"315\" src=\"" + m.group(0) + "\" frameborder=\"0\" allow=\"autoplayencrypted-media\" allowfullscreen></iframe>\n++++");
}
m.appendTail(sb);
Content = sb.toString();
Content = Content.replace("youtu.be", "youtube.com/embed");
Content = Content.replace("?t=", "?start=");
my-lord
  • 2,453
  • 3
  • 12
  • 26
0

You could use Java's replaceAll() method. It also uses Matcher().

A simple solution could be something like this:

String url = "https://www.youtube.com/watch?v=xcJtL7QggTI?start=60";

public String getEmbedString(String url){
  String embed = "iframe width="560" height="315" src="@" frameborder="0"allow="autoplay; encrypted-media" allowfullscreen></iframe>";
  embed.replaceAll("@", url);

  return embed;
}

I haven't tried to adapt my solution to your code, because this seems much simpler. Please let me know if this is what you were looking for.

  • This code converts all youtube URLs to the same URL. But there are different URLs in my text . – my-lord Aug 04 '18 at 13:07
  • I think @IamSousakotiris gave an example of how you could could convert your url's into an embedded url easily, just like you asked. He didn't say how to retrieve each url from the text file. You could adapt his/her answer to your solution – Soutzikevich Aug 04 '18 at 13:15
  • Yes. I am asking for converting URLs to an embedded URLs and I have done other processes in my program. That code will convert all the **different URLs** to this URL(https://www.youtube.com/watch?v=xcJtL7QggTI?start=60). – my-lord Aug 04 '18 at 13:28
  • 1
    Dear friend, it seems you are a little confused. Above code doesn't declare `String url;` as final. This means you should do it like **THIS**: `String url = whatever_url_you_read_from_your_file;` – Soutzikevich Aug 04 '18 at 15:06