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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Here a UDF

Put this behind a module. Then you can use it like

Excel Formula:
=jec(A1)

VBA Code:
Function jec(c As String) As String
 Dim it
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "[[[].*?[]]]"
   If .test(c) Then
     For Each it In .Execute(c)
       jec = Trim(jec) & " " & it
     Next
   End If
 End With
End Function
 
Upvote 1
Solution
Or with a formula, but will only work on a Windows PC.
Fluff.xlsm
AB
1
2This is [[Text 1]] followed by [[Text 2]], followed by several chars [[Text 3]] followed by....[[Text 1]] [[Text 2]] [[Text 3]]
Sheet5
Cell Formulas
RangeFormula
B2B2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(A2,"[[","]]"),"]]","</m><m>")&"</m></k>","//m"),"[["&TEXTJOIN("]] [[",,INDEX(f,SEQUENCE(ROWS(f)/2,,2,2)))&"]]")
 
Upvote 0
Here a UDF

Put this behind a module. Then you can use it like

Excel Formula:
=jec(A1)

VBA Code:
Function jec(c As String) As String
 Dim it
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "[[[].*?[]]]"
   If .test(c) Then
     For Each it In .Execute(c)
       jec = Trim(jec) & " " & it
     Next
   End If
 End With
End Function

Wow! thanks for responding so quickly. Today I learnt something new. It worked like a charm!
Thanks again as you've saved me a lot of time
 
Upvote 0
Or with a formula, but will only work on a Windows PC.
Fluff.xlsm
AB
1
2This is [[Text 1]] followed by [[Text 2]], followed by several chars [[Text 3]] followed by....[[Text 1]] [[Text 2]] [[Text 3]]
Sheet5
Cell Formulas
RangeFormula
B2B2=LET(f,FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(A2,"[[","]]"),"]]","</m><m>")&"</m></k>","//m"),"[["&TEXTJOIN("]] [[",,INDEX(f,SEQUENCE(ROWS(f)/2,,2,2)))&"]]")
Thanks Fluff for taking the time. Unfortunately i cannot try this as I have a Mac. However the solution provided by Jec worked so very grateful to both of you!
 
Upvote 0
I do have another formula based solution for you

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(r,MID(A2,UNIQUE(SEARCH("[[",A2&"[[",SEQUENCE(LEN(A2)))),SEQUENCE(,20)),TEXTJOIN(" ",,IF((LEFT(r,2)="[[")*(RIGHT(r,2)="]]"),r,"")))
 
Upvote 0
I do have another formula based solution for you

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(r,MID(A2,UNIQUE(SEARCH("[[",A2&"[[",SEQUENCE(LEN(A2)))),SEQUENCE(,20)),TEXTJOIN(" ",,IF((LEFT(r,2)="[[")*(RIGHT(r,2)="]]"),r,"")))
Cell Formulas
RangeFormula
Thanks for this. I think the VBA works better. Appreciate your response.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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