You are replacing (prefix)repl(suffix) with (prefix)vg(suffix) if both prefix and suffix match a pattern.
The prefix pattern is ((?:hares|hagrp)\s+(?:-add|-match|-link).*)
hares|hagrp matches hares or hagrp. | is an OR
\s+ looks for one or more (+) whitespace characters (\s)
(?:) is needed to tell the OR that it will match hares or hagrp followed by \s+, not hares or hagrp\s+. The ?: makes this non-capturing.
The pattern is surrounded by parenthesis, so it is captured as a group.
The suffix pattern is (.*$)
.* captures any number of any character.
$ matches the end of the line.
The pattern is surrounded by parenthesis, so it is captured as a group.
replace: '\1vg\2' replaces the string with a new string if the pattern match was successful.
\1 refers to the first captured group, \2 the second. If the match was successful, the replacement string is:
(prefix group)vg(suffix group)
Using snippsat's example: "hagrp -add some configuration repl with these settings"
The prefix group is "hagrp -add some configuration "
The suffix group is " with these settings"
The new string is "hagrp -add some configuration " + "vg" + " with these settings"