RegExp constructor
Constructs a regular expression.
Throws a FormatException if source
does not follow valid regular
expression syntax.
If your code enables multiLine
, then ^
and $
will match
the beginning and end of a line, as well as matching beginning and
end of the input, respectively.
If your code disables caseSensitive
,
then Dart ignores the case of letters when matching.
For example, with caseSensitive
disable, the regexp pattern a
matches both a
and A
.
If your code enables unicode
, then Dart treats the pattern as a
Unicode pattern per the ECMAScript standard.
If your code enables dotAll
, then the .
pattern will match all
characters, including line terminators.
Example:
final wordPattern = RegExp(r'(\w+)');
final digitPattern = RegExp(r'(\d+)');
These examples use a raw string as the argument.
You should prefer to use a raw string as argument to the RegExp
constructor, because it makes it easy to write
the \
and $
characters as regexp reserved characters.
The same examples written using non-raw strings would be:
final wordPattern = RegExp('(\\w+)'); // Should be raw string.
final digitPattern = RegExp('(\\d+)'); // Should be raw string.
Use a non-raw string only when you need to use string interpolation. For example:
Pattern keyValuePattern(String keyIdentifier) =>
RegExp('$keyIdentifier=(\\w+)');
When including a string verbatim into the regexp pattern like this, be careful that the string does not contain regular expression reserved characters. If that risk exists, use the escape function to convert those characters to safe versions of the reserved characters and match only the string itself:
Pattern keyValuePattern(String anyStringKey) =>
RegExp('${RegExp.escape(anyStringKey)}=(\\w+)');
Implementation
external factory RegExp(String source,
{bool multiLine = false,
bool caseSensitive = true,
@Since("2.4") bool unicode = false,
@Since("2.4") bool dotAll = false});