The `stringMatch` method in Dart's `RegExp` class returns the first substring match of the regular expression in a given input string.
The syntax of RegExp.stringMatch() method is:
String stringMatch(String input)
This stringMatch() method of RegExp returns the first substring match of this regular expression in input
.
Parameter | Optional/Required | Description |
---|---|---|
input | required | The input string in which to search for the first substring match. |
RegExp.stringMatch() returns value of type String
.
In this example,
void main() {
RegExp pattern = RegExp(r'\d+');
String text = '123 abc 456 def';
String? match = pattern.stringMatch(text);
if (match != null) {
print('First match: $match');
} else {
print('No match found.');
}
}
First match: 123
In this example,
void main() {
RegExp pattern = RegExp('hello');
String text = 'hello world';
String? match = pattern.stringMatch(text);
if (match != null) {
print('First match: $match');
} else {
print('No match found.');
}
}
First match: hello
In this Dart tutorial, we learned about stringMatch() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.