Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that scans a text file for possible e-mail addresses and writes all email address to an output file.

Addresses look like this:

someone@somewhere.net


What I have tried:

Java
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class EmailidExtration {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
 
        Pattern pattern = Pattern.compile("([a-z0-9_.-]+)@([a-z0-9_.-]+[a-z])");
        Matcher matcher = pattern.matcher("emailtext.txt");
 
        while(matcher.find()){
            System.out.println(matcher.group());
        }
    }
}
Posted
Updated 16-Nov-20 20:52pm
v2
Comments
Patrice T 16-Nov-20 20:58pm    
What the problem is with this code ?
Member 14994674 17-Nov-20 9:10am    
it outputs nothing...it runs but do not return a thing
Patrice T 17-Nov-20 9:53am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Your code finds nothing, because your matcher is working on the filename, not the file content:
Java
Matcher matcher = pattern.matcher("emailtext.txt");
Read the file, and pass the content to the matcher instead of the filename.

And a better email Regex is:
([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900