import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "Gas Commercial Service \\([\\S\\s]+?(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))?(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))?(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))?(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))?(?:[\\s]+(?:(?:(?:[\\w]+ )*)?(?:[\\w]+)?Tax:[xX\\d\\.\\%\\s]*?\\$[\\d\\.\\s]*?\\$([\\d\\.]*)\\s*?))?";
final String string = " Due Date: 03/13/13\n"
+ "Electric Commercial Service (E2C)\n"
+ "Meter Number: 298209\n"
+ "Reading 02/26/13 91391 Access Chg: 30 days x $0.6324 $18.97\n"
+ "Reading 01/27/13 90150 Access Chg: 1,241 kWh x $0.0532 $66.02\n"
+ " 1241 Kilowatt Hours(KWH) Supply Chg: 1,241 kWh x $0.0269 $33.38\n"
+ "Your average daily usage was 41.37 KWH ECA: 1,241 kWh x $-0.001 $1.24 CR\n"
+ " Capacity Chg: 1,241 kWh x $0.0012 $1.49\n"
+ " City Sales Tax: 2.5% x $118.62 $2.97\n"
+ " County Sales Tax: 1.23% x $118.62 $1.46\n"
+ " State Sales Tax: 2.9% x $118.62 $3.44\n"
+ " PPRTA Tax: 1% x $118.62 $1.19\n"
+ " ECA = Electric Cost Adjustment\n"
+ " - Making energy-efficiency improvements to your\n"
+ " business? Save more money with rebates. Visit\n"
+ " csu.org/business for details.\n"
+ " Total charge this service $127.68\n\n"
+ "Gas Commercial Service (G1C)\n"
+ "Meter Number: 73757\n"
+ "Reading 02/26/13 25459 Access Chg (G1CL): 30 days x $0.7623 $22.87\n"
+ "Reading 01/27/13 25277 Access Chg (G1CL): 182 CCF x $0.1571 $28.59\n"
+ " 182 Hundred Cubic Feet(CCF) Gas Cost (G1CL): 182 CCF x $0.6034 $109.82\n"
+ "Your average daily usage was 6.07 CCF GCA (G1CL): 182 CCF x $-0.17 $30.94 CR\n"
+ " City Sales Tax: 2.5% X $130.34 $3.26\n"
+ " County Sales Tax: 1.23% X $130.34 $1.60\n"
+ " State Sales Tax: 2.9% X $130.34 $3.78\n"
+ " BullSheet Federal SuperTax: 2.9% X $130.34 $3.78\n"
+ " PPRTA Tax: 1% X $130.34 $1.30\n"
+ " - Smell a rotten egg odor at your business? You may\n"
+ " have a natural gas leak. Leave the premise\n"
+ " immediately, then call 9-1-1. Emergency crews will\n"
+ " respond.\n"
+ " Total charge this service $140.28";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
if (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