VOOZH about

URL: https://www.geeksforgeeks.org/dsa/extracting-all-email-ids-in-any-given-string-using-regular-expressions/

⇱ Extracting all Email Ids in any given String using Regular Expressions - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Extracting all Email Ids in any given String using Regular Expressions

Last Updated : 23 Jul, 2025

Given a string str, the task is to extract all the Email ID's from the given string.

Example:

Input: "Please send your resumes to Hr@Iwillgetbacktoyou@gmail.com for any business inquiry please mail us at business@enquiry@gmail.com"
Output: Hr@Iwillgetbacktoyou@gmail.com
business@enquiry@gmail.com

Approach: The problem can be solved based on the following idea:

Create a regex pattern to validate the number as written below:   
regex = “^[0-9]{3}[A-Z]{2}[0-9]{8}$

Where,

  • $: Matches the end of the line
  • \s: Matches whitespace
  • \S: Matches any non-whitespace character
  • *: Repeats a character zero or more times
  • \S: Matches any non-whitespace character
  • *?: Repeats a character zero or more times (non-greedy)
  • +: Repeats a character one or more times
  • +?: Repeats a character one or more times (non-greedy)
  • [aeiou]: Matches a single character in the listed set
  • [^ABC]: Matches a single character, not in the listed set
  • [a-z0-9]: The set of characters can include a range
  • (: Indicates where string extraction is to start
  • ): Indicates where string extraction is to end

Follow the below steps to implement the idea:

  • Create a regex expression to extract all the Email Ids from the string.
  • Use Pattern class to compile the regex formed.
  • Use the matcher function to find.

Below is the code implementation of the above-discussed approach:


Output
Hr@Iwillgetbacktoyou@gmail.com
business@enquiry.com

Time Complexity: O(n)

Space Complexity: O(n)

Related Articles:

Comment
Article Tags: