import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(.+\\s-\\s\\w{2})$|^(Total\\s\\d{1,5}:.+)$";
final String string = "Total\n"
+ "2015\n"
+ "DOLLAR\n"
+ "AMOUNT\n"
+ "Oct-15 DIFF 15-16\n"
+ "Total\n"
+ "2015\n"
+ "COMPANY 1 - WI\n"
+ "Nuts $ 59.85 $ 0.00 $ 135.45 $ 0.00 $ 135.45 $0.00\n"
+ "Bolts $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
+ "Screws $ 449.20 $ 0.00 $ 541.23 $ 0.00 $ 541.23 $0.00\n"
+ "Total 7765: $ 509.05 $ 0.00 $ 676.68 $ 0.00 $ 676.68 $0.00\n"
+ "Company 2 - NE\n"
+ "Nuts $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
+ "Bolts $ 0.00 $ 55.67 $ 542.48 $ 719.82 $(177.34) $777.02\n"
+ "Total 1876: $ 0.00 $ 55.67 $ 2,960.52 $ 4,265.82 $(1,305.30) $5,854.60\n"
+ "Company 3 - MN\n"
+ "Nuts $ 109.52 $ 606.52 $ 858.36 $ 606.52 $ 251.84 $606.52\n"
+ "Paper $ 0.00 $ 0.00 $ 483.82 $ 0.00 $ 483.82 $678.30\n"
+ "Gas $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
+ "OTHER $ 521.49 $ 0.00 $ 521.49 $ 0.00 $ 521.49 $0.00\n"
+ "Total 6524: $ 631.01 $ 606.52 $ 3,909.09 $ 606.52 $ 3,302.57 $1,284.82\n"
+ "Company 4 - IA\n"
+ "Anything $ 0.00 $ 0.00 $ 0.00 $ 0.00 $ 0.00 $0.00\n"
+ "Other $ 0.00 $ 0.00 $ 171.90 $ 0.00 $ 171.90 $0.00\n"
+ "Total 1123: $ 0.00 $ 0.00 $ 171.90 $ 0.00 $ 171.90 $0.00";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html