0

I'm trying to convert *this function to Dart with no success so far. I'm a bit confused on how the regex part. My implementation using regExp.allMatches***(url)*** aways returns the whole url.

Javascript version:

function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    return (match&&match[7].length==11)? match[7] : false;
}

WIP Dart version:

  RegExp regExp = new RegExp(
    r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
    caseSensitive: false,
    multiLine: false,
  );

  final match = regExp.allMatches(url);

Can anyone help me on that?

EDIT:

Input URLs:

  final urls = [
    'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
    'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
    'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
    'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
    'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
    'http://www.youtube.com/watch?v=0zM3nApSvMg',
    'http://youtu.be/0zM3nApSvMg',
  ];

Output for getYoutubeVideoId(url) for each should be 0zM3nApSvMg

Thanks in advance,

Felipe

felipebueno
  • 330
  • 1
  • 3
  • 15

1 Answers1

5

Well, I do not have the complete solution but I hope this will help you, Your problem is you are using a RegExp which returns the result as a group which you need to select:

final urls = [
  'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
  'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
  'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
  'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
  'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
  'http://www.youtube.com/watch?v=0zM3nApSvMg',
  'http://youtu.be/0zM3nApSvMg',
];

void main() {
  RegExp regExp = new RegExp(
    r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*',
    caseSensitive: false,
    multiLine: false,
  );

  for (final url in urls) {
    final match = regExp.firstMatch(url).group(1); // <- This is the fix
    print('$url -> $match');
  }
}

This code will return:

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index -> 0zM3nApSvMg
http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o -> QdK8U-VIH_o
http://www.youtube.com/v/0zM3nApSvMg?fs=1&amp;hl=en_US&amp;rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s -> 0zM3nApSvMg
http://www.youtube.com/embed/0zM3nApSvMg?rel=0 -> 0zM3nApSvMg
http://www.youtube.com/watch?v=0zM3nApSvMg -> 0zM3nApSvMg
http://youtu.be/0zM3nApSvMg -> 0zM3nApSvMg

As you can see, there are still some problems with some of the URL's but I hope my solution will help you to get the RegExp working.

julemand101
  • 28,470
  • 5
  • 52
  • 48
  • https://www.youtube.com/live/rhFusVkBkgs?feature=share, how to get id from this type of link, basically this kind of links are available in youtubes when program are going live – Taki Rajani Feb 25 '23 at 19:54
  • @TakiRajani You can change the regular expression to: `RegExp(r'.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|live\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*', caseSensitive: false, multiLine: false)` – julemand101 Feb 25 '23 at 20:20
  • Thankyou soo much sir, its work i am voting your answer. – Taki Rajani Feb 25 '23 at 20:38