I have the following Java code:
String initial = "Phone number: [194-582-9412]";
System.out.println(initial.replaceAll("\\d{3}\\-\\d{3}(?=\\-\\d{4})","XXX-XXX"));
System.out.println(initial.replaceAll("\\d{3}\\-\\d{3}(?:\\-\\d{4})","XXX-XXX"));
which produces output:
Phone number: [XXX-XXX-9412]
Phone number: [XXX-XXX]
My logic was to find 3 digits, a dash, 3 digits (capturing to this point), a dash, and four digits (non-capturing to this point). According to this tutorial, lookahead groups starting with ?= are non-capturing. According to the Pattern Javadoc, groups beginning with ?: are also non-capturing. I expected both regular expressions to produce the same output, Phone number: [XXX-XXX-9412]. However, the regular expression with the non-capturing group (?:\\-\\d{4}) seems to capture the entire phone number and replace it. Why is this happening?
?:here, because it was the first option that popped to mind, and it didn't work as expected.