Replacing Multiple Patterns in a Single Pass

Problem

You want to perform multiple, simultaneous search-and-replace operations on a string.

Solution

Use the Regexp.union method to aggregate the regular expressions you want to match into one big regular expression that matches any of them. Pass the big regular expression into String#gsub, along with a code block that takes a MatchData object. You can detect which of your search terms actually triggered the regexp match, and choose the appropriate replacement term:

class String def mgsub(key_value_pairs=[].freeze) regexp_fragments = key_value_pairs.collect { |k,v| k } gsub( Regexp.union(*regexp_fragments)) do |match| key_value_pairs.detect{|k,v| k =~ match}[1] end end end

Here's a simple example:

"GO HOME!".mgsub([[/.*GO/i, 'Home'], [/home/i, 'is where the heart is']]) # => "Home is where the heart is!"

This example replaces all letters with pound signs, and all pound signs with the letter P:

"Here is number #123".mgsub([[/[a-z]/i, '#'], [/#/, 'P']]) # => "#### ## ###### P123"

 

Discussion

The naive solution is to simply string together multiple gsub calls. The following examples, copied from the solution, show why this is often a bad idea:

"GO HOME!".gsub(/.*GO/i, 'Home').gsub(/home/i, 'is where the heart is') # => "is where the heart is is where the heart is!" "Here is number #123".gsub(/[a-z]/i, "#").gsub(/#/, "P") # => "PPPP PP PPPPPP P123"

In both cases, our replacement strings turned out to match the search term of a later gsub call. Our replacement strings were themselves subject to search-and-replace. In the first example, the conflict can be fixed by reversing the order of the substitutions. The second example shows a case where reversing the order won't help. You need to do all your replacements in a single pass over the string.

The mgsub method will take a hash, but it's safer to pass in an array of key-value pairs. This is because elements in a hash come out in no particular order, so you can't control the order of substution. Here's a demonstration of the problem:

"between".mgsub(/ee/ => 'AA', /e/ => 'E') # Bad code # => "bEtwEEn" "between".mgsub([[/ee/, 'AA'], [/e/, 'E']]) # Good code # => "bEtwAAn"

In the second example, the first substitution runs first. In the first example, it runs second (and doesn't find anything to replace) because of a quirk of Ruby's Hash implementation.

If performance is important, you may want to rethink how you implement mgsub. The more search and replace terms you add to the array of key-value pairs, the longer it will take, because the detect method performs a set of regular expression checks for every match found in the string.

See Also

Категории