Substitute multiple words in one go

Hi,
Is there a way to substitute multiple word in a string in one go? e.g. I have a string "Quick brown fox jumps over the lazy dog", I want to substitute the "brown", "fox" and "dog" into "black", "dog" and "sheep" respectively hence the sentence turn into "Quick black dog jumps over the lazy sheep".

P.S. the number of replacements can be different depends on the case.

Thanks

OriginalPostID-268847

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    You can use this for dynamic number of replacements. Create rule inputs accordingly. subString and replacement are text array inputs.

    Example: 

    Inputs:
    ri!sentence: Quick brown fox jumps over the lazy dog 

    ri!subString: {"brown","fox","dog"}

    ri!replacement: {"black","dog","sheep"}

    if(
      count(ri!subString) <> count(ri!replacement),
      "Invalid replacement configuration. Please ensure that replacements are correctly specified.",
      reduce(
        fn!substitute,
        ri!sentence,
        merge(ri!subString, ri!replacement)
      )
    )

     

Reply
  • 0
    Certified Senior Developer

    You can use this for dynamic number of replacements. Create rule inputs accordingly. subString and replacement are text array inputs.

    Example: 

    Inputs:
    ri!sentence: Quick brown fox jumps over the lazy dog 

    ri!subString: {"brown","fox","dog"}

    ri!replacement: {"black","dog","sheep"}

    if(
      count(ri!subString) <> count(ri!replacement),
      "Invalid replacement configuration. Please ensure that replacements are correctly specified.",
      reduce(
        fn!substitute,
        ri!sentence,
        merge(ri!subString, ri!replacement)
      )
    )

     

Children