Regexreplace multiple strings with a loop?

Hello!

Is it possible to use substituteAll() or regexreplace() recursively in a loop on the same original “haystack” text?

The goal is to use replace/substitute to wrap multiple words in a paragraph with brackets.

So, for example, after selecting “red” and “nose” in Column A, Column B would be "Rudolph the [red] nosed reindeer had a very shiny [nose].

I can use regexreplace() or substituteAll() for this, but only one word at a time. To wrap multiple words in brackets, I would need to nest multiple substituteAll()s. But I want this to be dynamic, i.e., I want it to work with a varying number of words selected.

So I think we need to use a loop that can iteratively/recursively wrap each word from the list in brackets. For this we can use formulaMap().

But this formula:

thisRow.[Words to Wrap in Brackets].FormulaMap(
  SubstituteAll(thisRow.[Original Paragraph],CurrentValue,"["+CurrentValue+"]")
)

produces this output:

Rudolph the [red] nosed reindeer had a very shiny nose.,Rudolph the red [nose]d reindeer had a very shiny [nose].

Because it’s recursive, maybe we can use withName(), but I don’t know if withName can redefine already-named variables.

Here’s a test doc. Feel free to make a copy.

Thanks for your help!

I did this once a long time ago

I think the easiest way is that if every target you are after is a single word, simply Split(“ “) Which will turn your paragraph into a list of individual words, then run a forEach() Statement, and for each loop use a switch if statement that conditionally replaces the current value with your bracketed target value

Or by default return the current word if it’s not a target word.

Then join with a “ “

That will work, if the targets are after not individual words, you can do it with a button in a single helper column that recursively sets the value of the column, replaces the new word, and then resets it

Let me know if you need more help

Oh my god, I just found that Coda does have support for regex capturing groups.
I’ve been trying \1 since I’m used to Python, but apparently $1 is the way to go, like in Javascript!

Anyway, this makes this problem much easier!

thisRow.[Original Paragraph].RegexReplace("(" + thisRow.[Words to Wrap in Brackets].Join("|") + ")", "[$1]")
3 Likes

the non-regex idiom is split(‘xxx’).join(‘yyy’) which replaces all instances of xxx with yyy.
so you just chain these pairs together.

it may not be a fast (measured in micro-seconds) as regex, but its easier to remember.

That’s a pretty clever workaround! But I believe the new SubstituteAll replaces the need for that right?

1 Like

a clever solution for this specific use case:

thisRow.[Original Paragraph].RegexReplace("(" + thisRow.[Words to Wrap in Brackets].Join("|") + ")", "[$1]")

I wonder why @Jon_Dallas needs the , can you enlighten us?

1 Like

@Rickard_Abraham , @Xyzor_Max ,

The RegEx from Rickard allows for multiple search and replaces in one operation, at least the add-brackets type operation. With The CFL functions like ReplaceAll you have to run as many iterations as there are search words and if you don’t work with a temp column you get the repeats of the original text, as mentioned earlier.

I think the RegEx the way it is presented by Rickard is one of those gems we every so often run into - thanks.

Greetings, Joost

3 Likes

Hey, thanks so much for the solutions ya’ll.

@Rickard_Abraham’s solution seems to be most well suited for my purposes here. Just gave it a try and it works perfectly. Really awesome find!

@Christiaan_Huizer, as for the specific use-case, I am building a Claude-based resume optimizer. The final resume will contain some fabricated information about the candidate. So I have a helper prompt determine which information was fabricated, then I will use this formula to wrap that information in brackets [like this] to indicate that it’s a placeholder.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.