A regular expression match.
Regular expression matches are Matches, but also include the ability to retrieve the names for any named capture groups and to retrieve matches for named capture groups by name instead of their index.
Example:
const pattern =
r'^\[(?<Time>\s*((?<hour>\d+)):((?<minute>\d+))\.((?<second>\d+)))\]'
r'\s(?<Message>\s*(.*)$)';
final regExp = RegExp(
pattern,
multiLine: true,
);
const multilineText = '[00:13.37] This is a first message.\n'
'[01:15.57] This is a second message.\n';
RegExpMatch regExpMatch = regExp.firstMatch(multilineText)!;
print(regExpMatch.groupNames.join('-')); // hour-minute-second-Time-Message.
final time = regExpMatch.namedGroup('Time'); // 00:13.37
final hour = regExpMatch.namedGroup('hour'); // 00
final minute = regExpMatch.namedGroup('minute'); // 13
final second = regExpMatch.namedGroup('second'); // 37
final message =
regExpMatch.namedGroup('Message'); // This is a first message.
final date = regExpMatch.namedGroup('Date'); // Undefined `Date`, throws.
Iterable<RegExpMatch> matches = regExp.allMatches(multilineText);
for (final m in matches) {
print(m.namedGroup('Time'));
print(m.namedGroup('Message'));
// 00:13.37
// This is a first message.
// 01:15.57
// This is a second message.
} name. group.
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-core/RegExpMatch-class.html