Dart语言,如何从字符串中提取特定部分
Dart language, How to extract specific part from a string
我想从完整的字符串中提取这部分:
<a href=\"https://example.com/members/will/\">Will</a>
完整字符串:
"<a href=\"https://example.com/members/will/\">Will</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"
另一个例子
<a href=\"https://example.com/members/longerName/\">longerName</a>
完整字符串:
"<a href=\"https://example.com/members/longerName/\">longerName</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"
请帮忙
从我的头脑来看,这可行。
正则表达式
<a\b[^>]*>(.*?)</a>
飞镖
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));
});
这可行:
String str = "<a href=\"https://example.com/members/will/\">Will</a> posted an update in the group <a
href=\"https://example.com/groups/testing/\">Testing</a>";
String result = str.split("</a>")[0] + "</a>";
我想从完整的字符串中提取这部分:
<a href=\"https://example.com/members/will/\">Will</a>
完整字符串:
"<a href=\"https://example.com/members/will/\">Will</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"
另一个例子
<a href=\"https://example.com/members/longerName/\">longerName</a>
完整字符串:
"<a href=\"https://example.com/members/longerName/\">longerName</a> posted an update in the group <a href=\"https://example.com/groups/testing/\">Testing</a>"
请帮忙
从我的头脑来看,这可行。
正则表达式
<a\b[^>]*>(.*?)</a>
飞镖
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));
});
这可行:
String str = "<a href=\"https://example.com/members/will/\">Will</a> posted an update in the group <a
href=\"https://example.com/groups/testing/\">Testing</a>";
String result = str.split("</a>")[0] + "</a>";