This method replaces the first substring of the subject string, which matches the given regular expression with the replacement String. As the name implies, replaceFirst replaces only the first matching occurrence of the input String. Note that we can also supply a simple String containing no regex meta character in the first argument as regex.
Calling this method is equivalent to the following call:
Pattern.compile(regex).matcher(input).replaceFirst(replacement);
A replacement string allows the use of group references, such as $1, $2, $3, and so on, for numbered references or ${groupName} for named references, which represent each of the captured groups in the regular expressions. To place a literal dollar or a literal backslash in the replacement, we need to escape these characters using the double backslash.