Skip to content

Instantly share code, notes, and snippets.

@azeezat
Last active March 20, 2020 13:29
Show Gist options
  • Save azeezat/2e0fce9c84fa12add080a50b7f7fdfbe to your computer and use it in GitHub Desktop.
Save azeezat/2e0fce9c84fa12add080a50b7f7fdfbe to your computer and use it in GitHub Desktop.
Regex in Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
class RegexExample
{
public static void main(String args[])
{
//An array list to create a list of emails
List emails = new ArrayList();
emails.add("raheem.azeezat@venturegardengroup.com");
emails.add("raheem.azeezat@yahoo.co.uk");
emails.add("raheem.azeezat@venturegardengroupcom");
//Invalid emails
emails.add("raheemazeezat#venturegardengroup.com");
emails.add("@venturegardengroup.com");
String regex = "^(.+)@(.+)$";
Pattern pattern = Pattern.compile(regex);
for(String email : emails){
Matcher matcher = pattern.matcher(email);
System.out.println(email +" : "+ matcher.matches());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment