Flutter & Dart Regular Expression Examples

Flutter & Dart: Regular Expression Examples

Example 1: Email Verification

coding:

void main() {
  RegExp exp = new RegExp(
      r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$",
      caseSensitive: false);// Try it
  String email1 = "[email protected]";
  String email2 = "test@gmail";
  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");
  }
}

output:

Email1 OK
Email2 OK

Example 2: IP address verification

coding:

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
  if(ipExp.hasMatch('192.168.1.2')){
    print('192.168.1.2 is valid'); 
  }
  
  // Expectation: false
  if(ipExp.hasMatch('192.168.111111.55555')){
    print('192.168.111111.55555 is valid');
  }
}

output:

192.168.1.2 is valid

Example 3: URL Validation

coding:

void main() {
  RegExp urlExp = RegExp(r"(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?");
  
  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!");
  }
}

output:

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
  
  if(domainExp.hasMatch(domain1)){
    print('domain1 is valid');
  }
  
  if(domainExp.hasMatch(domain2)){
    print('domain2 is valid');
  }
  
  if(domainExp.hasMatch(domain3)){
    print('domain3 is valid');
  }
}

output:

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.

example

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) {
    print(product.name);
  }
}

output:

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

Reference : removeWhere method (dart.dev).

Summarize

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 *