Extracting matched expressions, Email Address Finder - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Description

Extracting matched expressions, Email Address Finder

Demo Code

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

public class Main {
  public static void main(String[] argv) {
    Pattern pattern = Pattern.compile(
        "([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})(\\]?)", Pattern.MULTILINE);

    Matcher matcher = pattern.matcher("");
    boolean matched = false;
    matcher.reset("asdfs@java2s.com");
    while (matcher.find()) {
      System.out.println("\t" + matcher.group());

      matched = true;//from  ww  w .j  a v a  2s.c om
    }
    if (!matched) {
      System.out.println("\tNo email addresses found");
    }
  }
}

Result


Related Tutorials