filter out password in xml string We're storing the xml communications with an external service in a text in the db and in a before_create I've got the following:

  # filter opera password
  def remove_password!
    self.request.gsub!  /UserPassword\&gt;[^\&lt;]*\&lt;\//,
                        'UserPassword&gt;[FILTERED]&lt;/'
  end


Is there a better, safer way of doing it?
That depends on how confident you are that neither of these cases will be true:


Another tag like  will be introduced that you will want to leave unfiltered
No nested tags will appear within the tag


An example of the last would be:

&lt;UserPassword&gt;&lt;for name="boy"&gt;Sue&lt;/for&gt;&lt;/UserPassword&gt;


It seems your assumptions are safe, but if you want to be certain, go with

self.request.gsub! /\&lt;UserPassword\&gt;.+?\&lt;/UserPassword\&gt;/gm, '&lt;UserPassword&gt;[FILTERED]&lt;/UserPassword&gt;'


By eliminating the greediness of the regex, you can match the exact tag you mean and reduce the assumptions that will come back and bite you.