I have a file which have a series of multiple email addresses. I need to separate emails from it using Regex in java. I have taken a sample string and tried like below.
import java.util.HashSet;
import java.util.Set;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Email2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String content = "s.lokesh1729@gmail.com,lokesh.harshitha1729@gmail.com";
Pattern p = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher m = p.matcher(content);
if(m.find())
{
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
}
The above code doesn't returns anything, but if I changed content to containing only one email address, it returns. I know that I can split it with the comma or any other special character, but I want to do it with Regular Expressions?
You could split the email addresses in content, see below:
String string = "s.lokesh1729@gmail.com,lokesh.harshitha1729@gmail.com";
String[] array= string.split(",");
String array= array[0]; // s.lokesh1729@gmail.com
String array= array[1]; // lokesh.harshitha1729@gmail.com
Then, you could test your email against this expression:
String REGEX = "([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";
And here you can see how they're evaluated How to validate email address with regular expression
Snippets below are from URL above.
EmailValidator.java
package com.mkyong.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidator {
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN);
}
/**
* Validate hex with regular expression
*
* @param hex
* hex for validation
* @return true valid hex, false invalid hex
*/
public boolean validate(final String hex) {
matcher = pattern.matcher(hex);
return matcher.matches();
}
}
But finally, here they validate multiple email addresses with a regular expression How to validate multiple email addresses with regular expression in Java
Snippet below is from URL above:
public static boolean isValid(String email) {
System.out.println(email + " >>>>>>>> is Valid? " + Pattern.matches(regex, email));
return Pattern.matches(regex, email);
}
Not sure on Java, but you'd have to sit in a while loop looking for all the
emails.
Something like while( search( Rx, string ) ) { ... }
This is a good regex from here .
"[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*"
Formatted:
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+
@
[a-zA-Z0-9]
(?:
[a-zA-Z0-9-]{0,61}
[a-zA-Z0-9]
)?
(?:
\.
[a-zA-Z0-9]
(?:
[a-zA-Z0-9-]{0,61}
[a-zA-Z0-9]
)?
)*
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.