The `hasMatch` method in Dart's `RegExp` class checks whether the regular expression has a match in the specified input string.
The syntax of RegExp.hasMatch() method is:
bool hasMatch(String input)
This hasMatch() method of RegExp returns whether the regular expression has a match in the string input
.
Parameter | Optional/Required | Description |
---|---|---|
input | required | The input string to search for a match. |
RegExp.hasMatch() returns value of type bool
.
In this example,
void main() {
RegExp regex = RegExp(r'\d+');
String text = 'Today is 25th December 2023';
bool hasMatch = regex.hasMatch(text);
print('Regex has match: $hasMatch');
}
Regex has match: true
In this example,
void main() {
RegExp regex = RegExp(r'[A-Z][a-z]+');
String text = 'The quick Brown Fox jumps over the lazy Dog';
bool hasMatch = regex.hasMatch(text);
print('Regex has match: $hasMatch');
}
Regex has match: true
In this example,
void main() {
RegExp regex = RegExp(r'\W+');
String text = 'Hello World!';
bool hasMatch = regex.hasMatch(text);
print('Regex has match: $hasMatch');
}
Regex has match: true
In this Dart tutorial, we learned about hasMatch() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.