The `allMatches` method in Dart's `RegExp` class returns an iterable of matches of the regular expression on the specified input string.
The syntax of RegExp.allMatches() method is:
Iterable<RegExpMatch> allMatches(String input, [ int start = 0 ])
This allMatches() method of RegExp returns an iterable of the matches of the regular expression on input
.
Parameter | Optional/Required | Description |
---|---|---|
input | required | The input string on which to search for matches. |
start | optional | The starting position in the input string from which to begin the search. The default is 0. |
RegExp.allMatches() returns value of type Iterable<RegExpMatch>
.
In this example,
void main() {
RegExp regex = RegExp(r'\w+');
String text = 'Hello World!';
Iterable<RegExpMatch> matches = regex.allMatches(text);
for (RegExpMatch match in matches) {
print('Match: ${match.group(0)}');
}
}
Match: Hello Match: World
In this example,
void main() {
RegExp regex = RegExp(r'\d+');
String text = 'Today is 25th December 2023';
Iterable<RegExpMatch> matches = regex.allMatches(text);
for (RegExpMatch match in matches) {
print('Match: ${match.group(0)}');
}
}
Match: 25 Match: 2023
In this example,
void main() {
RegExp regex = RegExp(r'[A-Z][a-z]+');
String text = 'The quick Brown Fox jumps over the lazy Dog';
Iterable<RegExpMatch> matches = regex.allMatches(text);
for (RegExpMatch match in matches) {
print('Match: ${match.group(0)}');
}
}
Match: The Match: Brown Match: Fox Match: Dog
In this Dart tutorial, we learned about allMatches() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.