Reputation: 865
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
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