Flutter & Dart Regular Expression Examples

Flutter & Dart: Regular Expression Examples

Example 1: Email Verification


void main() {
  RegExp exp = new RegExp(
      r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
      caseSensitive: false);// Try it
  String email1 = "[email protected]";
  String email2 = "[email protected]";
  String email3 = "test#gmail.com";if (exp.hasMatch(email1)) {
    print("Email1 OK");
  }if (exp.hasMatch(email2)) {
    print("Email2 OK");
  }if (exp.hasMatch(email3)) {
    print("Email3 OK");


Email1 OK
Email2 OK

Example 2: IP address verification


void main() {
  RegExp ipExp = new RegExp(r"^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$", caseSensitive: false, multiLine: false);
  // Try it
  // Expectation: true
    print(' is valid'); 
  // Expectation: false
    print('192.168.111111.55555 is valid');

output: is valid

Example 3: URL Validation


void main() {
  RegExp urlExp = RegExp(r"(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?");
  String url1 = "https://www.kindacode.com/cat/mobile/flutter/"; // valid
  String url2 = "https://kindacode/cat/mobile/flutter/"; // invalid
  if(urlExp.hasMatch(url1)) {
    print('Url1 looks good!');
  if(urlExp.hasMatch(url2)) {
    print("Url2 looks good!");


Url1 looks good!

Example 4: Domain Validation

void main() {
  RegExp  domainExp = RegExp(r"^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}$");
  String domain1 = "www.kindacode.com"; // valid
  String domain2 = "www.amazon.co.uk"; // valid
  String domain3 = "hello world!"; // invalid
    print('domain1 is valid');
    print('domain2 is valid');
    print('domain3 is valid');


domain1 is valid
domain2 is valid

Flutter & Dart: Conditionally remove elements from a list

To remove elements from a list based on one or more conditions, you can use the built-in removeWhere() method.


Suppose we have a list of products and the task is to remove all products with a price higher than 100:

// Define how a product looks like
class Product {
  final double id;
  final String name;
  final double price;
  Product(this.id, this.name, this.price);

void main() {
  // Generate the product list
  List<Product> products = [
    Product(1, "Product A", 50),
    Product(2, "Product B", 101),
    Product(3, "Product C", 200),
    Product(4, "Product D", 90),
    Product(5, "Product E", 400),
    Product(6, "Product F", 777)

  // remove products whose prices are more than 100
  products.removeWhere((product) => product.price > 100);

  // Print the result
  print("Product(s) whose prices are less than 100:");
  for (var product in products) {


Product(s) whose prices are less than 100:
Product A
Product D

Reference : removeWhere method (dart.dev).


We’ve gone through some examples of using regular expressions in Dart that can be very useful in many common use cases. If you want to learn more about Dart and Flutter, you can follow me.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *