The syntax of String.allMatches() method is:
Iterable<Match> allMatches(String string, [int start = 0])
This allMatches() method of String matches this pattern against the string repeatedly.
Parameter | Optional/Required | Description |
---|---|---|
string | required | matching is done against this string |
start | optional [default value is 0] | if provided, matching will start at this index |
String.allMatches() returns value of type Iterable<Match>
.
In this example,
regex
to match one or more digits.allMatches()
method with the string 'Today is 2024-05-02 and 42 is the answer.'void main() {
RegExp regex = RegExp(r'\d+');
Iterable<Match> matches = regex.allMatches('Today is 2024-05-02 and 42 is the answer.');
for (Match match in matches) {
print('Match found: ${match.group(0)}');
}
}
Match found: 2024 Match found: 05 Match found: 02 Match found: 42
In this example,
regex
to match any alphabet character.allMatches()
method with the string 'Hello World!'void main() {
RegExp regex = RegExp(r'[A-Za-z]');
Iterable<Match> matches = regex.allMatches('Hello World!');
for (Match match in matches) {
print('Match found: ${match.group(0)}');
}
}
Match found: H Match found: e Match found: l Match found: l Match found: o Match found: W Match found: o Match found: r Match found: l Match found: d
In this example,
regex
to match one or more alphabetic characters.allMatches()
method with the string 'The quick brown fox jumps over the lazy dog.'void main() {
RegExp regex = RegExp(r'[a-zA-Z]+');
Iterable<Match> matches = regex.allMatches('The quick brown fox jumps over the lazy dog.');
for (Match match in matches) {
print('Match found: ${match.group(0)}');
}
}
Match found: The Match found: quick Match found: brown Match found: fox Match found: jumps Match found: over Match found: the Match found: lazy Match found: dog
In this Dart tutorial, we learned about allMatches() method of String: the syntax and few working examples with output and detailed explanation for each example.