Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Dart: RegEx to extract URLs from a String

This is my string:

    window.urlVideo = 'https://node34.vidstreamcdn.com/hls/5d59908aea5aa101a054dec2a1cd3aff/5d59908aea5aa101a054dec2a1cd3aff.playlist.m3u8';
var playerInstance = jwplayer("myVideo");
var countplayer = 1;
var countcheck = 0;
playerInstance.setup({
    sources: [{
        "file": urlVideo
    }],
    tracks: [{
        file: "https://cache.cdnfile.info/images/13f9ddcaf2d83d846056ec44b0f1366d/12.vtt",
        kind: "thumbnails"
    }],
    image: "https://cache.cdnfile.info/images/13f9ddcaf2d83d846056ec44b0f1366d/12_cover.jpg",
});

function changeLink() {
    window.location = "//vidstreaming.io/load.php?id=MTM0OTgz&title=Mairimashita%21+Iruma-kun+Episode+12";
}
window.shouldChangeLink = function () {
    window.location = "//vidstreaming.io/load.php?id=MTM0OTgz&title=Mairimashita%21+Iruma-kun+Episode+12";
}

I am using flutter dart.

How can I get window.urlVideo URL link and image URL link and .vtt file link?

Or

How can I get a list of URLs from a String? I tried finding a way with and without using RegEx but I couldn't.

Any help is apreciated

like image 655
dheeraj reddy Avatar asked Dec 22 '19 13:12

dheeraj reddy


1 Answers

This may not be the complete regex, but this worked for me for randomly picked links:

void main() {
  final text = """My website url: https://blasanka.github.io/
Google search using: www.google.com, social media is facebook.com, http://example.com/method?param=flutter
stackoverflow.com is my greatest website. DartPad share: https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide see this example and edit it here https://dartpad.dev/3d547fa15849f9794b7dbb8627499b00""";

  RegExp exp = new RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
  Iterable<RegExpMatch> matches = exp.allMatches(text);

  matches.forEach((match) {
    print(text.substring(match.start, match.end));
  });
}

Result:

https://blasanka.github.io/
www.google.com
facebook.com
http://example.com/method?param=flutter
stackoverflow.com
https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide
https://dartpad.dev/3d547fa15849f9794b7dbb8627499b00

Play with it here: https://dartpad.dev/3d547fa15849f9794b7dbb8627499b00

like image 126
Blasanka Avatar answered Sep 22 '22 00:09

Blasanka