Dart RegExp toString()
Syntax & Examples
RegExp.toString() method
The `toString` method in Dart's `RegExp` class returns a string representation of the `RegExp` object.
Syntax of RegExp.toString()
The syntax of RegExp.toString() method is:
String toString()
This toString() method of RegExp returns a string representation of this object.
Return Type
RegExp.toString() returns value of type String
.
✐ Examples
1 String Representation of RegExp for Matching Words
In this example,
- We create a regular expression `regex` to match words.
- We print the string representation of the regex using the `toString` method.
Dart Program
void main() {
RegExp regex = RegExp('hello');
print('String representation of regex: ${regex.toString()}');
}
Output
String representation of regex: RegExp/hello/
2 String Representation of RegExp for Matching Numbers
In this example,
- We create a regular expression `regex` to match numbers.
- We print the string representation of the regex using the `toString` method.
Dart Program
void main() {
RegExp regex = RegExp('[0-9]+');
print('String representation of regex: ${regex.toString()}');
}
Output
String representation of regex: RegExp/[0-9]+/
3 String Representation of RegExp for Matching Alphabets
In this example,
- We create a regular expression `regex` to match alphabetic characters.
- We print the string representation of the regex using the `toString` method.
Dart Program
void main() {
RegExp regex = RegExp('[a-zA-Z]+');
print('String representation of regex: ${regex.toString()}');
}
Output
String representation of regex: RegExp/[a-zA-Z]+/
Summary
In this Dart tutorial, we learned about toString() method of RegExp: the syntax and few working examples with output and detailed explanation for each example.