Use only one gsub and its block parameter `matched`(String) to check index specification in block, instead of using global variable `$~`:
```ruby
# not tested code
v = v.gsub(/__TAG_PARTS\[-?[0-9]+(\.\.\.?-?[0-9]+)?\]__|\$\{tag_parts\[-?[0-9]+(\.\.\.?-?[0-9]+)?\]\}/) do |matched_string|
  if matched_string =~ /\[-?[0-9]+\.\.\.?-?[0-9]+\]/ # range pattern
    # generate range pattern tag strings....
  else
    # return a part of tag
  end
end
```