getMatches method
Returns a list of the matches found in input
.
start
and stop
refer to the indexes of the delimited blocks
of text. Only matches found between start
and stop
will be
returned.
start
must not be null
and must be >= 0
.
stop
may be null
and must be >= start
.
If reverse
is true
, input
's delimited blocks of text
will be parsed in reverse order. If looking for a match
towards the end of the string, finding it in reverse order
is more efficient. start
and stop
will be counted in
the order the matches are closed.
Implementation
List<RegExpMatch>? getMatches(
String input, {
int start = 0,
int? stop,
bool reverse = false,
}) {
assert(start >= 0);
assert(stop == null || stop >= start);
final delimiters = _getDelimiters(input);
if (delimiters == null) return null;
if (delimiters.length == 2) {
if (start > 1) return null;
return regExp.allMatches(input).toList();
}
final matches = <RegExpMatch>[];
var index = 0;
String getMatch(Match start, Match end) =>
input.substring(0, start.start).clean() +
input.substring(start.start, end.end);
final openDelimiters = <Delimiter>[];
for (var delimiter in reverse ? delimiters.reversed : delimiters) {
if ((!reverse && delimiter.position == DelimiterPosition.start) ||
(reverse && delimiter.position == DelimiterPosition.end)) {
openDelimiters.add(delimiter);
continue;
}
if (global || inverseMatch != null || openDelimiters.length == 1) {
if (index >= start && (stop == null || index <= stop)) {
final startDelimiter =
reverse ? delimiter.match : openDelimiters.last.match;
final endDelimiter =
reverse ? openDelimiters.last.match : delimiter.match;
final match = getMatch(startDelimiter, endDelimiter);
matches.add(regExp.firstMatch(match)!);
}
index++;
if (stop != null && index > stop) break;
}
openDelimiters.removeLast();
}
if (inverseMatch != null) {
return _getInverseMatches(input, matches);
}
if (matches.isEmpty) {
return null;
}
return matches;
}