If you're using python, you can use backslash substitution like in re.sub using the Match.expand() method. This means that you don't need to capture the entire string. An example is as follows:
import re
in_str = '<h1> this is valid html</h1>name="some_text_0_some_text"'
use_reg = 'name="(\w+)(\d+)(\w+)"'
replace_str = r"\1!NEW_ID!\3"
def find_with_replace_option(use_str, use_reg, to_str):
""" Find matches of the regex use_reg in the string use_str. Return
to_str if there are any matches. to_str may contain backslash
substitution.
"""
result_list = []
for match in re.finditer(use_reg,use_str):
result = match.expand(to_str)
result_list.append(result)
return result_list
print(find_with_replace_option(in_str,use_reg,replace_str))
Here the regex works as follows: the first part "some_text_" is captured in the first group, the zero is captured in the second group and the last part "_some_text" becomes the third group.
replace_str specifies that the output should consist of the first group, followed by "!NEW_ID!" followed by the third group.
The result is some_text_!NEW_ID!_some_text as expected