Examples of Regex in Java or is EMail valid?

java-regex.png

Introduction

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.

Java provides the java.util.regex package for pattern matching with regular expressions. It is widely used to define constraint on strings such as password and email validation.

java.util.regex package provides following classes and interface for regular expressions. The Matcher and Pattern classes are widely used in Java regular expression.

  • MatchResult interface
  • Matcher class
  • Pattern class
  • PatternSyntaxException class

Matcher class implements MatchResult interface. It is a regex engine i.e. used to perform match operations on a character sequence.

  • matches() method tests whether the regular expression matches the pattern.
  • find() method finds the next expression that matches the pattern.
  • find(int start) method finds the next expression that matches the pattern from the given start number.
  • group() method returns the matched subsequence.
  • start() method returns the starting index of the matched subsequence.
  • end() method returns the ending index of the matched subsequence.
  • groupCount() method returns the total number of the matched subsequence.

Pattern class is the compiled version of a regular expression. It is used to define a pattern for the regex engine.

  • compile(String regex) method compiles the given regex and return the instance of pattern.
  • matcher(CharSequence input) method creates a matcher that matches the given input with pattern.
  • matches(String regex, CharSequence input) method works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.
  • split(CharSequence input) method splits the given input string around matches of given pattern.
  • pattern() method returns the regex pattern.

The easiest way to check if a regular expression pattern matches a text is to use the static Pattern.matches() method. Here is a Pattern.matches() example in Java code:

String s = "There's no time like the present!";
String regex = ".*time.*";
boolean matches = Pattern.matches(regex, s);
System.out.println("matches = " + matches);

The Pattern.matches() method is fine if you just need to check a pattern against a text a single time, and the default settings of the Pattern class are appropriate.

If you need to match a text against a regular expression pattern more than one time, you need to create a Pattern instance using the Pattern.compile() method. Here is a Java Pattern.compile() example:

String s = "There's no time like the present!";
String regex = ".*time.*";
Pattern pattern = Pattern.compile(regex);

The split() method in the Pattern class can split a text into an array of String's, using the regular expression (the pattern) as delimiter. Here is a Java Pattern.split() example:

String s = "A, B, C, D, E, F, G";

String regex = ",";
Pattern pattern = Pattern.compile(regex);
String[] split = pattern.split(s);
System.out.println("split.length = " + split.length);

Validate Email Address

Email validation using regular expressions is common task which may be required in any application which seek email address as required information in registration step.

List emails = new ArrayList();
emails.add("user@server.ua");
emails.add("man@server.in.ua");
emails.add("woman@domain.com");

// invalid emails
emails.add("user.domain.com");

String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
Pattern pattern = Pattern.compile(regex);

for(String email : emails) {
    Matcher matcher = pattern.matcher(email);
    System.out.println(email + " : " + matcher.matches());
}

Date Format Validation

Here is regex to match dd/mm/yyyy with required leading zeros

List dates = new ArrayList();
dates.add("07/13/2011");
dates.add("13/07/2011");
dates.add("1/1/11");
dates.add("01/1/2011");

String regex = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";
Pattern pattern = Pattern.compile(regex);
for(String date : dates) {
    Matcher matcher = pattern.matcher(date);
    System.out.println(date +" : "+ matcher.matches());
}

Validate International Phone Numbers

List phones = new ArrayList();
phones.add("+1 1234567890123");
phones.add("+12 123456789");
phones.add("+123 123456");

String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$";
Pattern pattern = Pattern.compile(regex);
for(String phone : phones) {
    Matcher matcher = pattern.matcher(phone);
    System.out.println(phone +" : "+ matcher.matches());
}

Validate Credit Card Numbers

Each of the credit card companies uses followin number format.

  • Visa card has 13 or 16 digits, starting with 4.
  • MasterCard has 16 digits, starting with 51 through 55.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;

List<String> cards = new ArrayList<String>();

// valid
cards.add("4111-1111-1111-1111");

// invalid 
cards.add("1111-1111-1111-1111");

String regex = "^(?:(?<visa>4[0-9]{12}(?:[0-9]{3})?)|" +
        "(?<mastercard>5[1-5][0-9]{14}))";

Pattern pattern = Pattern.compile(regex);

for (String card : cards) {
    card = card.replaceAll("-", "");
    Matcher matcher = pattern.matcher(card);
    System.out.println(matcher.matches());
}
comments powered by Disqus