10 C
New York
Monday, November 27, 2023

Flutter & Dart: Regular Expression Examples

A few examples of regular expression and the RegExp class in Flutter and Dart.

Example 1: Email Validation

The code:Advertisements

void main() {
  RegExp exp = new RegExp(
      caseSensitive: false);

  // Try it
  String email1 = "test@gmail.com";
  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");


Email1 OK
Email2 OK

Example 2: IP Addresses Validation

The code:

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

AdvertisementsThe code:

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!");


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

Wrap Up


We’ve gone through a few examples of using regular expression in Dart that may be very helpful in many common use cases. If you’d like to learn more about Dart and Flutter, take a look at the following articles:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.

Related Articles

Latest Articles