Dart RegExp matchAsPrefix()
Syntax & Examples
RegExp.matchAsPrefix() method
The `matchAsPrefix` method of Dart's `RegExp` class matches this pattern against the start of a string.
Syntax of RegExp.matchAsPrefix()
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
.
Parameters
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. |
Return Type
RegExp.matchAsPrefix() returns value of type Match
.
✐ Examples
1 Matching a digit sequence at the start
In this example,
- We create a `RegExp` object named `pattern` with the pattern '\d+' to match one or more digits.
- We define a `text` string containing alphanumeric characters.
- We use the `matchAsPrefix()` method to match the digit sequence at the start of the `text` string.
- If a match is found, we print the matched prefix using `match.group(0)`; otherwise, we print 'No match found.'
Dart Program
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.');
}
}
Output
Matched prefix: 123
2 Matching a word sequence at the start
In this example,
- We create a `RegExp` object named `pattern` with the pattern '[a-z]+' to match one or more lowercase letters.
- We define a `text` string containing the phrase 'hello world'.
- We use the `matchAsPrefix()` method to match the word sequence at the start of the `text` string.
- If a match is found, we print the matched prefix using `match.group(0)`; otherwise, we print 'No match found.'
Dart Program
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.');
}
}
Output
Matched prefix: hello
3 Matching a digit sequence starting from index 3
In this example,
- We create a `RegExp` object named `pattern` with the pattern '^\d+' to match one or more digits at the start of the string.
- We define a `text` string containing alphanumeric characters.
- We use the `matchAsPrefix()` method to match the digit sequence starting from index 3 in the `text` string.
- If a match is found, we print the matched prefix using `match.group(0)`; otherwise, we print 'No match found.'
Dart Program
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.');
}
}
Output
No match found.
Summary
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.