Dart:
Using regular expressions
How to:
Dart uses the RegExp
class for regular expressions. Here’s a basic example to match a simple pattern within a string:
void main() {
var pattern = RegExp(r'\bDart\b');
var text = 'Learning Dart programming is exciting.';
if (pattern.hasMatch(text)) {
print('Match found!');
} else {
print('No match found.');
}
// Output: Match found!
}
To extract matches from a string, you can use the allMatches
method. This method returns an iterable of matches:
void main() {
var pattern = RegExp(r'\b\w+\b');
var text = 'Dart is awesome!';
var matches = pattern.allMatches(text);
for (final match in matches) {
print(match.group(0)); // This prints the matched substrings.
}
// Output:
// Dart
// is
// awesome
}
Replacing text can be achieved using the replaceFirst
or replaceAll
methods:
void main() {
var pattern = RegExp(r'\bDart\b');
var text = 'Dart is not just a dart.';
// Replace first occurrence
var modifiedText = text.replaceFirst(pattern, 'Flutter');
print(modifiedText);
// Output: Flutter is not just a dart.
// Replace all occurrences
modifiedText = text.replaceAll(pattern, 'Flutter');
print(modifiedText);
// Output: Flutter is not just a flutter.
}
Splitting a string by a regex pattern is straightforward using the split
method:
void main() {
var pattern = RegExp(r'\s+'); // Matches any whitespace character
var text = 'Dart is fun';
var parts = text.split(pattern);
print(parts);
// Output: [Dart, is, fun]
}
For complex parsing or validations not supported directly by Dart’s RegExp
, you might consider third-party libraries, but Dart’s standard library is often sufficient for common regex tasks, emphasizing its utility and versatility in handling regular expressions.