Dart - Using RegExp Examples

This tutorial explains how to perform pattern matching by using RegExp class in Dart along with some examples.

Regular expression (regex) is usually used to match a string against a pattern. Most programming languages support the use of regex. In Dart, it's available through RegExp class.

RegExp Class

In Dart, we can use RegExp class to find matches. To use the clas, first create an instance of it by calling the constructor

  external factory RegExp(String source,
      {bool multiLine: false, bool caseSensitive: true});

The parameter is the regular expression which is required. The other two parameters are optional.

If multiLine is set to true, it will match at the beginning and end of every line. Otherwise, it will only match at the beginning and end of the input.

If caseSensitive is set to true, it will ignore case.

Below are some of its methods that you need to know in order to perform pattern matching. As it implements Pattern, it also has Pattern's methods: allMatches and matchAsPrefix.

Method Description
bool hasMatch(String input); Returns whether at least a match occurs
Match firstMatch(String input); Returns the first match on the input
Iterable<Match> allMatches(String input, [int start = 0]); Returns all matches as an iterable.
Match matchAsPrefix(String string, [int start = 0]); Matches the pattern at the start of the string.
String stringMatch(String input); Returns the first match as string.

Match Class

As you can see on the RegExp's method list, some of them return Match or List<Match>. Therefore, you need to know the properties and methods of Match class to process the result. Below are the properties along with the description:

Property Description
int start; The index where the match starts.
int end; The index where the match end.
int groupCount; The number of matched group.
String input; The input string.
String pattern; The pattern.

And here's the methods

Method Description
String group(int group); Returns the string matched by the given group.
String operator [](int group); Returns the string matched by the given group.
List<String> groups(List<int> groupIndices); Returns a list of the groups with the given indices.

Examples

Below are some examples of using RegExp in Dart.

Find matches of one or more letter sequence

  RegExp re = RegExp(r'(\w+)');
  String str1 = "one two three";
  print('Has match: ${re.hasMatch(str1)}');
  // First match
  Match firstMatch = re.firstMatch(str1);
  print('First match: ${str1.substring(firstMatch.start, firstMatch.end)}');

  //  Iterate all matches
  Iterable matches = re.allMatches(str1);
  matches.forEach((match) {
    print(str1.substring(match.start, match.end));
  });

Output:

  Has match: true
  First match: one
  one
  two
  three

Make sure the string only contain alphanumeric characters

  String str1 = "abcd123";
  String str2 = "abcd123.";
  RegExp re = RegExp(r'^[a-zA-Z0-9]+$');
  print('Match str1: ${re.hasMatch(str1)}');
  print('Match str2: ${re.hasMatch(str2)}');

Output:

  Match str1: true
  Match str2: false

Make sure the string does not contain number

  String str1 = "abcde";
  String str2 = "abc1";
  RegExp re = RegExp(r'^\D+$');
  print('Match str1: ${re.hasMatch(str1)}');
  print('Match str2: ${re.hasMatch(str2)}');

Output:

  Match str1: true
  Match str2: false

Check if the string begins with a pattern

  String str1 = "abcde";
  String str2 = "zzabc";
  RegExp re = RegExp(r'abc');
  Match str1Match = re.matchAsPrefix(str1);
  Match str2Match = re.matchAsPrefix(str2);
  print('Match str1: ${str1Match != null}');
  print('Match str2: ${str2Match != null}');

Output:

  Match str1: true
  Match str2: false

Get the first matched string using stringMatch

  String str1 = "abcde qwerty";
  String str2 = "vwxyz uiop";
  RegExp re = RegExp(r'(\w+)');
  String str1Match = re.stringMatch(str1);
  String str2Match = re.stringMatch(str2);
  print('Match str1: $str1Match');
  print('Match str2: $str2Match');

Output:

  Match str1: abcde
  Match str2: vwxyz