import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { Pattern pattern = Pattern.compile("pattern"); Matcher matcher = pattern.matcher("infile.txt"); // Find all matches while (matcher.find()) { // Get the matching string String match = matcher.group(); } } }