013-004-009

Email Address Regex Validation

Medium

Problem Description

Email Address Regex Validation

In this problem: You will create a program that uses the Pattern and Matcher classes to validate email addresses and displays the results to standard output.

Learning Objective: Understand the basics of regex matching using Pattern and Matcher from the java.util.regex package

Overview

Regular expressions are a powerful tool for text pattern matching. In Java, the Pattern class compiles patterns, and the Matcher class performs matching operations.

In this problem, you will implement a simple email address validation using regular expressions.

Specifications

  1. Compile the regex pattern ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ using Pattern.compile()
  2. Validate the following 5 strings using Matcher's matches() method:
    • "user@example.com"
    • "admin@mail.co.jp"
    • "invalid-email"
    • "@nouser.com"
    • "test.user+tag@domain.org"
  3. For each string, output "<string>: valid" or "<string>: invalid"

Output Format

user@example.com: valid
admin@mail.co.jp: valid
invalid-email: invalid
@nouser.com: invalid
test.user+tag@domain.org: valid

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up