Home > Blockchain >  Dart language, How to extract specific part from a string
Dart language, How to extract specific part from a string

Time:05-08

I want to extract from the full String this part:

<a href=\"https://example.com/members/will/\">Will</a>

full String:

"<a href=\"https://example.com/members/will/\">Will</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"

Another example

    <a href=\"https://example.com/members/longerName/\">longerName</a>

full String:

"<a href=\"https://example.com/members/longerName/\">longerName</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"

any help please

CodePudding user response:

From the top of my head, this could work.

Regex

<a\b[^>]*>(.*?)</a>

Dart

final myString = "<a href=\"https://example.com/members/will/\">Will</a> bla bla bla";
final regexp = RegExp(r'<a\b[^>]*>(.*?)</a>'); 

// final match = regexp.firstMatch(myString);
// final link = match.group(0);

Iterable matches = regexp.allMatches(myString);
matches.forEach((match) {
  print(myString.substring(match.start, match.end));
});

  • Related