how to match email in regex in ruby

email_regex = /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\z/
  1. \A: Asserts the start of the string.

  2. [A-Za-z0-9._%+-]+: Matches one or more occurrences of letters (both uppercase and lowercase), digits, dots, underscores, percent signs, plus signs, and hyphens.

  3. @: Matches the at symbol.

  4. [A-Za-z0-9.-]+: Matches one or more occurrences of letters (both uppercase and lowercase), digits, dots, and hyphens.

  5. \.: Matches a literal dot.

  6. [A-Z|a-z]{2,}: Matches two or more occurrences of uppercase or lowercase letters. This part is used to match the top-level domain (TLD), which should have at least two characters.

  7. \z: Asserts the end of the string.

The entire regex pattern is designed to validate an email address by ensuring it follows a common pattern with a local part (username) followed by the at symbol and a domain part. The domain part includes a period and a TLD with at least two characters.