code: def replace_fields(meta, patterns):
      
      for pattern in patterns:
          try:
              field, regex, subst, _ = pattern.split(pattern[-1])
  
              # TODO: Allow numerical indices, and "+" for append
              namespace = meta
              keypath = [i.replace('\0', '.') for i in field.replace('..', '\0').split('.')]
              for key in keypath[:-1]:
                  namespace = namespace[key]
  
              namespace[keypath[-1]] = re.sub(regex, subst, namespace[keypath[-1]])
          except (KeyError, IndexError, TypeError, ValueError) as exc:
              raise error.UserError("Bad substitution '%s' (%s)!" % (pattern, exc))
  
      return meta
docs: Replace patterns in fields.