Q:

remove part of matcher after the match in regex pattern

I need to help in writing regex pattern to remove only part of the matcher from original string. 
Original String: 2017-02-15T12:00:00.268+00:00
Expected String: 2017-02-15T12:00:00+00:00
Expected String removes everything in milliseconds.  
My regex pattern looks like this:     (:[0-5][0-9])\.[0-9]{1,3}
i need this regex to make sure i am removing only the milliseconds from some time field, not everything that comes after dot.  But using above regex, I am also removing the minute part.  Please suggest and help. 

A:

You have defined a capturing group with (...) in your pattern, and you want to have that part of string to be present after the replacement is performed.  All you need is to use a backreference to the value stored in this capture.  It can be done with $1:
String s = "2017-02-15T12:00:00.268+00:00";
String res = s.replaceFirst("(:[0-5][0-9])\\.[0-9]{1,3}", "$1");
System.out.println(res); // => 2017-02-15T12:00:00+00:00

See the Java demo and a regex demo. 
The $1 in the replacement pattern tells the regex engine it should look up the captured group with ID 1 in the match object data.  Since you only have one pair of unescaped parentheses (1 capturing group) the ID of the group is 1.