Reputation: 2641
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?
Upvotes: 0
Views: 316
Reputation: 40024
You can actually do what you wanted using capturing groups. Here it captures the part you want to keep and replaces the whole string. The $1
is a back reference to the capture group.
System.out.println(
initial.replaceAll("\\d{3}-\\d{3}(\\-\\d{4})", "XXX-XXX$1"));
And I presume you realize that if the regex
doesn't match, then the original string is returned with no changes.
Upvotes: 1