Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

Is it possible to write such a formula that it will substitute in a text value all occurrences of few values with their replacements?

For example I would like to substitute all occurrences of aaa with 1 and bbb with 2. So that text aaabbbaaa will be changed to 121.


I have prepared an example spreadsheet which also has write access so feel free to add a column with your formula and in the answer just explain it. Also you can add new test cases.


In my “real world” case the look-up table is quite large (45 values) but could be considered “fixed”. So yes, I could just make a formula consisting of 45 nested SUBSTITUTE but such solution is obviously bad.

Also my real case is not as tricky as the example so there is no risk that replacement text will in any way match (even in parts) any of the values to be substituted. (In the example ddd is replaced with a which matches partially value aaa!) So if that causes problems you might skip the “feature”.

And finally in my real case I'm lucky enough to be able to split the text into parts which contain no more than one value to be replaced (and possibly some other character data!). The result could be then joined back into a single string. If that would make it any easier you could use that as well.


I tried using both SUBSTITUTE and REGEXREPLACE where text to be found and replacement text are arrays of values but that results in an array of results rather than multiple substitutions.

I tried also using REGEXREPLACE with VLOOKUP in replacement text that looked up for "$1" (the matched text) but that didn't work. The "$1" within VLOOKUP within REGEXREPLACE was treated literally rather than interpreted as matched text (as it would be if it was used in REGEXREPLACE directly).

share|improve this question
    
I also asked the question on Google forums: productforums.google.com/forum/#!category-topic/docs/…. –  Adam Badura Jul 19 '13 at 8:08
add comment

1 Answer

up vote 3 down vote accepted

As discussed in the thread you linked to in comments, the formula solution is quite convoluted and potentially very slow, if there are multiple instances to be replaced in the string. However with your qualification:

And finally in my real case I'm lucky enough to be able to split the text into parts which contain no more than one value to be replaced (and possibly some other character data!). The result could be then joined back into a single string. If that would make it any easier you could use that as well.

I believe the solution becomes a little more straight forward (and reasonably efficient, I would say):

=IFERROR(REGEXREPLACE(A2;JOIN("|";Lookup!$A$2:$A$5);VLOOKUP(REGEXEXTRACT(A2;JOIN("|";Lookup!$A$2:$A$5));Lookup!$A$2:$B$5;2;0)&"");A2)

which has the added advantage of being able to be easily converted into an array formula that will populate down the column:

=ArrayFormula(IFERROR(REGEXREPLACE(A2:A;JOIN("|";Lookup!A2:A5);VLOOKUP(REGEXEXTRACT(A2:A;JOIN("|";Lookup!A2:A5));Lookup!A2:B5;2*SIGN(ROW(A2:A));0)&"");A2:A))

I have also entered a custom function solution:

function MSUBSTITUTE(text, subTable)
{
  var searchArray = [], subArray = [];
  for (var i = 0, length = subTable.length; i < length; i++)
  {
    if (subTable[i][0])
    {
      searchArray.push(subTable[i][0]);
      subArray.push(subTable[i][1]);
    }
  }
  var re = new RegExp(searchArray.join('|'), 'g');
  return text.replace(re, function (match) {return subArray[searchArray.indexOf(match)];});
}

=MSUBSTITUTE(A2;Lookup!$A$2:$B$5)

It would be fairly trivial to convert this to an auto-populating array formula, as well.

share|improve this answer
    
Any future reader: also see more discussion on the Google forums: productforums.google.com/forum/#!category-topic/docs/… –  Adam Badura Jul 27 '13 at 21:25
    
Apparently the first formula should end with 1;0)&"");A2) not 2;0)&"");A2) (The "2" changes to a "1"). I haven't tested this myself, but you might want to double check. –  ChrisF Feb 5 at 12:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.