【问题标题】:How to extract substring starting from index of particular character upto the index of some other character from given string?如何从给定字符串中从特定字符的索引到其他字符的索引提取子字符串?
【发布时间】:2021-05-24 08:09:37
【问题描述】:

假设,我有一个字符串 str = "lorem ipsum src="https://www.youtube.com/embed/HyvFqW7xE1c" lorem ipsum"。我想提取子字符串 https://www.youtube.com/embed/HyvFqW7xE1c 这实际上是一个 url。我可以使用 indexOf() 方法提取所需网址的起始索引“h”的索引。但是如何提取长度不固定的完整网址。

【问题讨论】:

    标签: string flutter oop dart substring


    【解决方案1】:

    您可以使用RegExp()Iterable<RegExpMatch> 从字符串中获取URL。

      var text = """lorem ipsum src="https://www.youtube.com/watch?v=dQw4w9WgXcQ" lorem 
      ipsum""";
      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));
      });
    

    资源Here

    【讨论】:

    • tankyou 非常喜欢。你拯救了我的一天。
    • 我尝试访问从 www 开始的子字符串。但我得到了 //www.
    • 不,我认为您不会得到//,即使您以www. 开头的URL。
    猜你喜欢
    • 2012-10-30
    • 2011-09-27
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多