RegExp class Null safety

A regular expression pattern.

Regular expressions are Patterns, and can as such be used to match strings or parts of strings.

Dart regular expressions have the same syntax and semantics as JavaScript regular expressions. See ecma-international.org/ecma-262/9.0/#sec-regexp-regular-expression-objects for the specification of JavaScript regular expressions.

The firstMatch method is the main implementation method that applies a regular expression to a string and returns the first RegExpMatch. All other methods in RegExp can be build from that.

The following example finds the first match of a regular expression in a string.

RegExp exp = RegExp(r'(\w+)');
String str = 'Parse my string';
RegExpMatch? match = exp.firstMatch(str);
print(match![0]); // "Parse"

Use allMatches to look for all matches of a regular expression in a string.

The following example finds all matches of a regular expression in a string.

RegExp exp = RegExp(r'(\w+)');
String str = 'Parse my string';
Iterable<RegExpMatch> matches = exp.allMatches(str);
for (final m in matches) {
  print(m[0]);
}

The output of the example is:

Parse
my
string

Note the use of a raw string (a string prefixed with r) in the example above. Use a raw string to treat each character in a string as a literal character.

Implemented types

Constructors

RegExp(String source, {bool multiLine = false, bool caseSensitive = true, @Since("2.4") bool unicode = false, @Since("2.4") bool dotAll = false})
Constructs a regular expression.
factory

Properties

hashCode int
The hash code for this object.
read-onlyinherited
isCaseSensitive bool
Whether this regular expression is case sensitive.
read-only
isDotAll bool
Whether "." in this regular expression matches line terminators.
@Since("2.4")read-only
isMultiLine bool
Whether this regular expression matches multiple lines.
read-only
isUnicode bool
Whether this regular expression is in Unicode mode.
@Since("2.4")read-only
pattern String
The source regular expression string used to create this RegExp.
read-only
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

allMatches(String input, [int start = 0]) Iterable<RegExpMatch>
Matches this pattern against the string repeatedly.
override
firstMatch(String input) RegExpMatch?
Finds the first match of the regular expression in the string input.
hasMatch(String input) bool
Whether the regular expression has a match in the string input.
matchAsPrefix(String string, [int start = 0]) Match?
Matches this pattern against the start of string.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
stringMatch(String input) String?
The substring of the first match of this regular expression in input.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

escape(String text) String
Creates regular expression syntax that matches text.