I am trying to modify a list of strings to keep only the substring of each of them. Here is what I'm trying to do:
List<String> paychecks = new ArrayList<>();
paychecks.add("Paycheck_Box_EMP_61299_451");
paychecks.add("Paycheck_Box_EMP_5512_221");
paychecks.add("Paycheck_Box_EMP_99993_881");
paychecks.add("Paycheck_Box_EMP_831_141");
paychecks.replaceAll(paycheck -> paycheck.subString("insert here"))
I've tried to write something where it says "insert here" but it throws me errors or only red lines appear, but basically I want to take the substring of the paycheck ID after EMP_ and before the next _ . So ideally it should be like this:
[61299, 5512, 99993, 831]
Update (second attempt):
paychecks.forEach(paycheck ->
paycheck.replaceAll(paycheck, paycheck.substring(paycheck.indexOf("Paycheck_Box_"),
paycheck.indexOf("Paycheck_Box_" + "\\[(.*?)\\]" + "_")))))
Error thrown:
java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 26
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)

subString()? It'd probably be cleaner just to usereplaceAll()and a regexp pattern.people.replaceAll(person -> person.replaceAll("^.*EMP_", "").replaceAll("_.*$", ""));