The `matchAsPrefix` method of Dart's `RegExp` class matches this pattern against the start of a string.
The syntax of RegExp.matchAsPrefix() method is:
Match matchAsPrefix(String string, [ int start = 0 ])
This matchAsPrefix() method of RegExp match this pattern against the start of string
.
Parameter | Optional/Required | Description |
---|---|---|
string | required | The input string against which to match the pattern. |
start | optional | The index in the string at which to start matching. Default is 0. |
RegExp.matchAsPrefix() returns value of type Match
.
In this example,
void main() {
RegExp pattern = RegExp(r'\d+');
String text = '123abc';
Match? match = pattern.matchAsPrefix(text);
if (match != null) {
print('Matched prefix: ${match.group(0)}');
} else {
print('No match found.');
}
}
Matched prefix: 123
In this example,
void main() {
RegExp pattern = RegExp('[a-z]+');
String text = 'hello world';
Match? match = pattern.matchAsPrefix(text);
if (match != null) {
print('Matched prefix: ${match.group(0)}');
} else {
print('No match found.');
}
}
Matched prefix: hello
In this example,
void main() {
RegExp pattern = RegExp(r'^\d+');
String text = '123abc';
Match? match = pattern.matchAsPrefix(text, 3);
if (match != null) {
print('Matched prefix starting from index 3: ${match.group(0)}');
} else {
print('No match found.');
}
}
No match found.
In this Dart tutorial, we learned about matchAsPrefix() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.