rails字符串替換mgsub from ruby cookbook

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
Recipe 1.17, "Matching Strings with Regular Expressions"

Confused by the *regexp_fragments syntax in the call to Regexp.union? Take a look at Recipe 8.11, "Accepting or Passing a Variable Number of Arguments"
發佈了11 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章