Flutter Dart:从字符串中提取 URL 的正则表达式

dhe*_*ddy 8 regex dart flutter

这是我的字符串:

    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";
}
Run Code Online (Sandbox Code Playgroud)

我正在使用颤动飞镖。

如何获取 window.urlVideo URL 链接和图像 URL 链接以及 .vtt 文件链接?

或者

如何从字符串中获取 URL 列表?我尝试找到一种使用和不使用 RegEx 的方法,但我不能。

任何帮助都值得赞赏

Bla*_*nka 17

这可能不是完整的正则表达式,但这对我随机选择的链接有用:

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));
  });
}
Run Code Online (Sandbox Code Playgroud)

结果:

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
Run Code Online (Sandbox Code Playgroud)

在这里玩:https : //dartpad.dev/3d547fa15849f9794b7dbb8627499b00

  • 这应该被标记为正确答案! (2认同)

Cra*_*Cat 7

尝试这个,

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));
Run Code Online (Sandbox Code Playgroud)


小智 5

只获取https?和引号中的ftp url 是这样的:

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"

在第 2 组中捕获 URL 的位置。

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


E. *_*Sun 5

使用像linkify这样的库比滚动你自己的正则表达式要安全得多。

/// 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;
}
Run Code Online (Sandbox Code Playgroud)

  • Linkify 现在使用以下正则表达式: `r'^(.*?)((?:https?:\/\/|www\.)[^\s/$.?#].[^\s]* )'`。根据我在“https://regexr.com/3e6m0”的测试,这匹配“https://google.......totallyrealurl,当然!?” (2认同)