Dart RegExp by Examples
In this Dart tutorial, we learn how to use RegExp class to match Strings using regular expressions.
How to use firstMatch() method
In the following example Dart program we use the firstMatch() method to search the first match of the regular expression in the given String. The return RegExpMatch object can be used to get the substring that matches by start and end properties.
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString = "Dart programming language tutorials.";
RegExpMatch firstMatch = regExp.firstMatch(sampleString);
if(firstMatch != null) {
print("First match substring: ${sampleString.substring(firstMatch.start, firstMatch.end)}");
} else {
print("No matched.");
}
}
First match substring: Dart
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString = "!@#%^&*()";
RegExpMatch firstMatch = regExp.firstMatch(sampleString);
if(firstMatch != null) {
print("First match substring: ${sampleString.substring(firstMatch.start, firstMatch.end)}");
} else {
print("No matched.");
}
}
No matched.
How to use hasMatch() method
The hasMatch() method to return whether the regular expression has matched in a given String or not.
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString1 = "mobilecode.dev";
String sampleString2 = "%^&**";
bool hasMatch1 = regExp.hasMatch(sampleString1);
bool hasMatch2 = regExp.hasMatch(sampleString2);
print("Has match ${sampleString1}: ${hasMatch1}" );
print("Has match ${sampleString2}: ${hasMatch2}" );
}
Has match mobilecode.dev: true
Has match %^&**: false
How to use matchAsPrefix() method
The matchAsPrefix() method to search the match of the regular expression from in the String from a given index.
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString = "mobile code";
Match match = regExp.matchAsPrefix(sampleString, 7);
if(match != null) {
print("Match substring: ${sampleString.substring(match.start, match.end)}" );
}
}
Match substring: code
How to use stringMatch() method
The stringMatch() method returns the first substring result that matches the regular expression.
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString1 = "mobile code";
String sampleString2 = "^&**()";
String matchString1 = regExp.stringMatch(sampleString1);
String matchString2 = regExp.stringMatch(sampleString2);
print("Match of ${sampleString1}: ${matchString1}" );
print("Match of ${sampleString2}: ${matchString2}" );
}
Match of mobile code: mobile
Match of ^&**(): null
How to use allMatches() method
The allMatches() method to return a list of matches results of the regular expression.
void main() {
RegExp regExp = new RegExp(r"(\w+)");
String sampleString = "mobile code website";
Iterable<RegExpMatch> matches = regExp.allMatches(sampleString);
for(RegExpMatch match in matches) {
print("${sampleString.substring(match.start, match.end)}");
}
}
mobile
code
website
How to use escape() static method
The escape() static method can be used to escape the special characters that have special meaning in a regular expression from a specified String.
void main() {
print(RegExp.escape("test@mobilecode.dev"));
print(RegExp.escape("(sample*)"));
print(RegExp.escape("[test]"));
print(RegExp.escape("{sample}"));
print(RegExp.escape("1+1"));
print(RegExp.escape("?"));
print(RegExp.escape("^"));
print(RegExp.escape("|"));
print(RegExp.escape(r"\"));
}
test@mobilecode\.dev
\(sample\*\)
\[test\]
\{sample\}
1\+1
\?
\^
\|
\\
Happy Coding 😊
Related Articles
Dart String Data Type by Examples
Dart integer Data Type by Examples