Given a regex and a string s, I would like to generate a new string in which any substring of s matched by the regex is surrounded by parentheses.
For example: My original string s is “Alan Turing 1912-1954” and my regex happens to match “1912-1954”. The newly generated string should be “Alan Turing (1912-1954)”.
Solution 1:
\g<0>is a backreference to the entire match (\0doesn’t work; it would be interpreted as\x00).Solution 2: