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

ParameterOptional/RequiredDescription
stringrequiredThe input string against which to match the pattern.
startoptionalThe 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,

  1. We create a `RegExp` object named `pattern` with the pattern '\d+' to match one or more digits.
  2. We define a `text` string containing alphanumeric characters.
  3. We use the `matchAsPrefix()` method to match the digit sequence at the start of the `text` string.
  4. 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,

  1. We create a `RegExp` object named `pattern` with the pattern '[a-z]+' to match one or more lowercase letters.
  2. We define a `text` string containing the phrase 'hello world'.
  3. We use the `matchAsPrefix()` method to match the word sequence at the start of the `text` string.
  4. 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,

  1. We create a `RegExp` object named `pattern` with the pattern '^\d+' to match one or more digits at the start of the string.
  2. We define a `text` string containing alphanumeric characters.
  3. We use the `matchAsPrefix()` method to match the digit sequence starting from index 3 in the `text` string.
  4. 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.