import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "[A-Z]{2}\\d{4}(?=\\s\\d+)";
final String string = "Return to Contents\n\n"
+ "Streets Improvement Summary\n\n"
+ "Capital Expenses (1,000s) Total Prior Years FY 2021 FY 2022 FY 2023 FY 2024 FY 2025 Years 6-10 Beyond 10\n"
+ "Yrs\n\n"
+ "Ocotillo Road - Greenfield to Higley ST0540 66,519 332 12,413 53,774 - - - - -\n\n"
+ "Baseline Road - Burk to Consolidated Canal ST0710 1,340 87 1,253 - - - - - -\n\n"
+ "Baseline Road - Greenfield to Power ST0780 8,801 112 - - - - 8,689 - -\n\n"
+ "Recker Road - Ocotillo to Chandler Heights ST0800 11,611 - - - - - - 11,611 -\n\n"
+ "Hunt Highway - Val Vista to 164th ST0840 86,241 40 - - - - - - 86,201\n\n"
+ "Recker Road - Ray to SR 202 ST0960 3,940 455 3,485 - - - - - -\n\n"
+ "Higley Road - Riggs to Stacey ST0980 20,403 1,973 - - - - - 18,430 -\n\n"
+ "Ocotillo Road - 148th to Greenfield ST0990 20,317 - 5,568 14,749 - - - - -\n\n"
+ "Elliot Road - Neely to Burk ST1050 4,060 - - - - - - - 4,060\n\n"
+ "Val Vista Drive - Appleby to Riggs ST1120 34,277 7,513 26,764 - - - - - -\n\n"
+ "Lindsay Road - Queen Creek to Ocotillo ST1140 3,881 171 1,097 2,613 - - - - -\n\n"
+ "Hunt Highway - Higley to Recker ST1150 9,438 - - - - - - 9,438 -\n\n"
+ "Recker Road - Riggs to Hunt Highway ST1160 11,395 104 - - - - - 11,291 -\n\n"
+ "Lindsay Road - Pecos to SR 202 ST1170 12,171 6,609 5,562 - - - - - -\n\n"
+ "Warner Road - Power to East of Recker ST1180 14,752 - - - 14,752 - - - -\n\n"
+ "Power Road - Guadalupe to Santan Freeway ST1200 15,140 - - - 15,140 - - - -\n\n"
+ "Val Vista Drive - Riggs to Hunt Highway ST1270 7,779 - - - - - - 7,779 -\n\n"
+ "Warner and Greenfield Intersection ST1300 12,367 - - - - - - - 12,367\n\n"
+ "Ray and Gilbert Intersection ST1310 8,536 - - - - - - 8,536 -\n\n"
+ "Elliot and Gilbert Intersection ST1320 8,626 66 - - - - 8,560 - -\n\n"
+ "Guadalupe and Val Vista Intersection ST1330 8,880 - - - - - - 8,880 -\n\n"
+ "Guadalupe and Power Intersection ST1340 8,790 - - 8,790 - - - - -\n\n"
+ "Elliot and Cooper Intersection ST1380 10,370 670 9,700 - - - - - -\n\n"
+ "Elliot and Higley Intersection ST1390 7,552 - - - - - - 7,552 -\n\n"
+ "Germann Road - Gilbert to Val Vista ST1450 27,429 4,385 23,044 - - - - - -\n\n"
+ "Turn Lane Safety and Congestion Improvements ST1540 9,329 723 1,170 1,104 1,583 1,583 1,583 1,583 -\n\n"
+ "Accessibility Upgrades in Public ROW ST1550 2,209 1,278 586 345 - - - - -\n\n"
+ "Constellation Way - South of Coldwater ST1570 3,356 55 - - - - - 3,301 -\n\n"
+ "Lindsay Road/SR 202 Traffic Interchange ST1580 18,151 7,658 10,493 - - - - - -\n\n"
+ "Higley and Pecos Intersection ST1590 928 - - 928 - - - - -\n\n"
+ "Infrastructure Imp at Railroad Crossings ST1600 2,545 57 2,488 - - - - - -\n\n"
+ "Mesquite Street - Lindsay to Val Vista ST1620 6,107 - - - 6,107 - - - -\n\n"
+ "Adora Trails Improvements ST1660 874 830 44 - - - - - -\n\n"
+ "Val Vista Drive Reconstruction ST1690 7,405 491 6,914 - - - - - -\n\n"
+ "Val Vista Drive - Eastern Canal Bridge Repair ST1710 1,055 150 905 - - - - - -\n\n"
+ "Val Vista Lakes Improvements ST1720 48,195 480 64 12,147 8,860 10,276 16,368 - -\n\n"
+ "Higley and Baseline Intersection ST1740 5,028 4,714 314 - - - - - -\n\n"
+ "Guadalupe Road Improvements ST1770 9,920 - 1,750 8,170 - - - - -\n\n"
+ "PM10 Paving ST1790 3,283 131 52 1,512 - - - 1,588 -\n\n"
+ "Cooper Road - Encinas to Baseline ST1800 7,743 - - 7,743 - - - - -\n\n"
+ "Pecos Road - Allen to Lindsay ST1820 2,962 - 342 2,620 - - - - -\n\n"
+ "Higley Road - North of Elliot ST1830 1,523 - - 1,523 - - - - -\n\n"
+ "Cooley Station Transit Center ST1840 10,306 - - - - 10,306 - - -\n\n"
+ "Greenfield Road Right Turn Lane at Knox ST1850 613 - - 613 - - - - -\n\n"
+ "Gilbert and Warner Intersection ST1860 11,493 178 53 11,262 - - - - -\n\n"
+ "McQueen and Elliot Intersection ST1870 11,434 - - 11,434 - - - - -\n\n"
+ "Lindsay and Guadalupe Intersection ST1880 12,608 - - - - 12,608 - - -\n\n"
+ "Chandler Heights Improvements ST1890 3,000 - 3,000 - - - - - -\n\n"
+ "McQueen and Guadalupe Intersection ST1910 12,369 - 2,987 9,382 - - - - -\n\n"
+ "Val Vista and Ray Intersection ST1920 9,707 - - - - - - 9,707 -\n\n"
+ "Power and Queen Creek Intersection ST1940 8,537 - - - - - - 8,537 -\n\n"
+ "Market and Williams Field Interesction ST1960 813 48 39 - 726 - - - -\n\n"
+ "Val Vista and Mercy Intersection ST1970 1,172 48 39 1,085 - - - - -\n\n"
+ "Market and San Tan Village Intersection ST1980 1,342 55 35 - 1,252 - - - -";
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