Search code examples
regexgoogle-apps-scriptgoogle-sheetsgoogle-sheets-formula

Extract first/second/third word containing X in google sheets


Is it possible to extract pieces of text in a sentence containing X in a google sheet. In a Formula of via Apps script. I couldn't find a decent answer on google or didn't know what to search for.

For example:

A1: [Peter] kicks the [ball] to [Shaun].

I want to extract the first word containing '[' & ']' in B1, the second word containing '[' & ']' in C1, et cetera.

So: enter image description here

A B C D
[Peter] kicks the [ball] to [Shaun]. [Peter] [ball] [Shaun]

Solution

  • Try this

    =regexextract(A1,regexreplace(regexreplace(A1,"[\[]","(\\\["),"[\]]","\\\])"))