Extract data between several brackets within a cell

drumroll

New Member
Joined
Dec 5, 2023
Messages
7
Office Version
  1. 2021
Platform
  1. MacOS
Hello, I have looked at various posts and tried to use the formulas provided there but somehow I am unable to get the desired outcome. I have a long piece of text in a column which has various pieces of text within double brackets. I want to extracts all such occurrences where there is text within double brackets. The text within brackets can of of different lengths.

Column ADesired Outcome
This is [[Text 1]] followed by [[Text 2]], followed by several chars [[Text 3]] followed by....[[Text 1]] [[Text2]] [[Text 3]]

Thanks in advance for your help
 
This one is more stable than the last formula

Book1
AB
1
2This is [[Text 1]] followed by [[Text 2]], followed by several chars [[Text 3]] followed by....[[Text 1]] [[Text 2]] [[Text 3]]
Sheet1
Cell Formulas
RangeFormula
B2B2=LET(u,UNIQUE(SEARCH("[[",A2&"[[",SEQUENCE(LEN(A2)))),r,IFERROR(MID(A2,u,SEQUENCE(,LEN(A2))),""),TEXTJOIN(" ",,IF((LEFT(r,2)="[[")*(RIGHT(r,2)="]]")*(ISERROR(SEARCH(" [",r))),r,"")))
 
Upvote 1

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This one is more stable than the last formula

Book1
AB
1
2This is [[Text 1]] followed by [[Text 2]], followed by several chars [[Text 3]] followed by....[[Text 1]] [[Text 2]] [[Text 3]]
Sheet1
Cell Formulas
RangeFormula
B2B2=LET(u,UNIQUE(SEARCH("[[",A2&"[[",SEQUENCE(LEN(A2)))),r,IFERROR(MID(A2,u,SEQUENCE(,LEN(A2))),""),TEXTJOIN(" ",,IF((LEFT(r,2)="[[")*(RIGHT(r,2)="]]")*(ISERROR(SEARCH(" [",r))),r,"")))
Genius! Yes this worked as well though it looked like Excel was working hard to get the output :) Thanks so much!
 
Upvote 0
For those who like to avoid calling out to a RegExp engine, here is a solution employing VBA functionality only...
VBA Code:
Function DoubleBrackets(S As String) As String
  Dim X As Long, Arr As Variant
  Arr = Split(Replace(S, "]]", "[["), "[[")
  For X = 1 To UBound(Arr) Step 2
    DoubleBrackets = DoubleBrackets & " " & "[[" & Arr(X) & "]]"
  Next
  DoubleBrackets = Mid(DoubleBrackets, 2)
End Function
You would use this in cell like so...
=DoubleBrackets(A1)
 
Upvote 0
Here a oneliner :cool:

VBA Code:
Function jec(c As String) As String
jec = Join(Filter(Split(Replace(Replace(c, "]]", "]]^"), "[[", "^[["), "^"), "[["))
End Function
 
Upvote 0
Here a oneliner :cool:

VBA Code:
Function jec(c As String) As String
jec = Join(Filter(Split(Replace(Replace(c, "]]", "]]^"), "[[", "^[["), "^"), "[["))
End Function
Nice use of Filter. Why didn't you post this code in your first reply?
 
Upvote 0
Thanks! I usually go with regex. I didn’t think of this approach first🙂
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top