12

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

3
  • You say you are using Flutter/Dart but the code you shared is Javascript. Commented Dec 22, 2019 at 14:14
  • 1
    @JoãoSoares That is not the code. It is the value of the string. I want to scrape the urls from this string. Commented Dec 22, 2019 at 14:29
  • So your string is all this javascript code and you want to extract 3 URLs from it? Are you scrapping a web page? Surely there is a better way for you to obtain these urls without having to do this from your Flutter App. Commented Dec 22, 2019 at 14:45

5 Answers 5

41

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

Sign up to request clarification or add additional context in comments.

6 Comments

This should be marked as the correct answer!
I have a String Made by https://cretezy.com and Expected output: Made by <a href="https://cretezy.com">https://cretezy.com</a>. Kindly suggest me how can we do this? Thanks a lot.
Not worked for me. I have a string which has https://support.google.com/firebase?authuser=0#topic=6399725 url and your solution converts it to like https://support.google.com/firebase?authuser=0 ignoring hash and further text string #topic=6399725. Kindly suggest how can we solve it. Thanks a lot.
@Kamlesh you can add any special char you want to the reg expression as mentioned in above comments. Based on the special char you may have to use escape character
It also extract multi dots also. like goo... this.is
|
11

Try this,

final urlRegExp = new RegExp(
    r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?");
final urlMatches = urlRegExp.allMatches(text);
List<String> urls = urlMatches.map(
        (urlMatch) => text.substring(urlMatch.start, urlMatch.end))
    .toList();
urls.forEach((x) => print(x));

1 Comment

Thank you so much dear, your solution worked for me perfectly :)
7

Getting just the https? and ftp url's that are in quotes is this :

r"([\"'])\s*((?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zA-Z0-9\u00a1-\uffff]+-?)*[a-zA-Z0-9\u00a1-\uffff]+)(?:\.(?:[a-zA-Z0-9\u00a1-\uffff]+-?)*[a-zA-Z0-9\u00a1-\uffff]+)*(?:\.(?:[a-zA-Z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:\/(?:(?!\1|\s)[\S\s])*)?)\s*\1"

Where the Url is captured in group 2.

https://regex101.com/r/UPmLBl/1

Comments

6

Much safer to use a library like linkify instead of rolling your own regex.

/// Attempts to extract link from a string.
///
/// If no link is found, then return null.
String extractLink(String input) {
  var elements = linkify(input,
      options: LinkifyOptions(
        humanize: false,
      ));
  for (var e in elements) {
    if (e is LinkableElement) {
      return e.url;
    }
  }
  return null;
}

1 Comment

Linkify uses the following regex right now: r'^(.*?)((?:https?:\/\/|www\.)[^\s/$.?#].[^\s]*)'. Based on my testing at https://regexr.com/3e6m0, this matches https://google..............totallyrealurl,ofcourse!?
0

Blasanka's answer does not work for me because it recognizes "..." as a URL.

Instead, the following works for me ...

/// Implements https://stackoverflow.com/a/6041965.
Iterable<String> getUrls(String input) => RegExp(
      r'(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])',
    ).allMatches(input).map((match) => match.group(0)).nonNulls;

... and for the original poster's text.

void main() {
  getUrls(input).forEach(print);
}

// Text from the original poster
const input = '''
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";
}
''';

Result

https://node34.vidstreamcdn.com/hls/5d59908aea5aa101a054dec2a1cd3aff/5d59908aea5aa101a054dec2a1cd3aff.playlist.m3u8
https://cache.cdnfile.info/images/13f9ddcaf2d83d846056ec44b0f1366d/12.vtt
https://cache.cdnfile.info/images/13f9ddcaf2d83d846056ec44b0f1366d/12_cover.jpg

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.