prabin maharjan
prabin maharjan

Reputation: 865

How to extract substring starting from index of particular character upto the index of some other character from given string?

Suppose, i have a string str = "lorem ipsum src="https://www.youtube.com/embed/HyvFqW7xE1c" lorem ipsum". i want to extract substring https://www.youtube.com/embed/HyvFqW7xE1c which is actually a url. I can extract index of "h" which is starting index of the required url by using indexOf() method. But how can i extract complete url where length is url is not fixed.

Upvotes: 0

Views: 891

Answers (1)

Chirag Bargoojar
Chirag Bargoojar

Reputation: 1214

You can use RegExp() and Iterable<RegExpMatch> to get URL from string.

  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));
  });

Resource Here

Upvotes: 4

Related Questions