Validatio Velectrica: Email Input Validation I

Validatio, validationis (feminine noun) – validation; approval; confirmation.
Velectrica, velectricae (neuter noun) – electric.

Table of Contents

Input validation lets us ensure only properly formed data is entering the workflow in an information system, preventing malformed data from reaching the database and triggering malfunction of various downstream components. RegEx is a powerful pattern-matching engine designed to search and match complex patterns: From email addresses to IP addresses to phone numbers to infinitely complex text patterns.

In this Amulet, we’ll use regular expressions to match some email addresses.

Problem Statement

We are presented with a set of email addresses containing different structures. A sample of the complete set can be found below:

Our job is to match all the valid email addresses, using whichever flavor suits us best. A typical email address is composed of the following parts:

  • A unique username: dougie.jones
  • A domain name: luckyseven
  • A top-level domain name: .com

Requirements

  • Only the valid addresses must be matched.
  • Each address must be a complete and single match, meaning we must not do partial matches on fragments of the address.
  • Our expression has to be flexible, meaning it has to account for all possible and top-level domains.
  • We must name each part of the email address with its respective section. That means:
    • Username
    • Domain name
    • Top-level domain name


Tackling Groups

Tackling group by group and composing a final expression in the end can significantly ease the task.




The RegEx expression can be written in any flavor using any tool. However, the solution only includes the expression in PCRE2 flavor (PHP >=7.3).

References

Related

In the first part of this 3-article series, we introduced the concepts of columnar file formats & row-based file formats. We also…
Formally, a type class is a type-system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables…
We live in an era where reliable data has become an invaluable asset. We’re constantly bombarded with information from…
Over the past decade, programming has emerged as an indispensable tool, enabling us to tackle challenges previously thought impossible….

All content on this post is licensed under a Creative Commons Attribution 4.0 International license.

Request Full Resume