Extract Email from Text
This program extracts email address from text using regex.
Problem Statement
Write a Java program to extract email from "Contact us at info@eduinq.com".
Source Code
| 1 | import java.util.regex.*; |
| 2 | public class RegexExtractEmail { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Regex Extract Email"); |
| 5 | String str="Contact us at info@eduinq.com"; |
| 6 | Pattern p=Pattern.compile("[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+"); |
| 7 | Matcher m=p.matcher(str); |
| 8 | if(m.find()) System.out.println("Extracted email: "+m.group()); |
| 9 | } |
| 10 | } |
Program Output
Eduinq Regex Extract Email Extracted email: info@eduinq.com
Explanation
A Pattern matching a general email format is compiled, and the Matcher's find() method locates the first matching sequence within the larger text, extracting the embedded email address.