Extract text in between square brackets []

ahmedismailfourtex

Board Regular
Joined
Apr 28, 2015
Messages
124
hello,
how to extract text in between [] using PQ or any technique ?, and if i have more than one [] in the same cell what to do to extract both separately or at least the first one ?
for example [abc] and i need abc
thanks:)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Re: Extract text in between square pracits []

Have you tried the text to columns feature using the square brackets as the separator?
 
Upvote 0
Last edited:
Upvote 0
Re: Extract text in between square pracits []

Thank you Istvan for your reply, but i need PQ or PP solution.
Enter the formula in B1 (if the string is in A1) and copy across as long as needed:

*ABCD
1blabla and [abc] and [defg] endabcdefg*

<colgroup><col style="width:30px; "><col style="width:207px;"><col style="width:60px;"><col style="width:86px;"><col style="width:64px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
B1=TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"[","]"),"]",REPT(" ",LEN($A1))),(2*COLUMNS($A:A)-1)*LEN($A1),LEN($A1)))
C1=TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"[","]"),"]",REPT(" ",LEN($A1))),(2*COLUMNS($A:B)-1)*LEN($A1),LEN($A1)))
D1=TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"[","]"),"]",REPT(" ",LEN($A1))),(2*COLUMNS($A:C)-1)*LEN($A1),LEN($A1)))

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Upvote 0
Re: Extract text in between square pracits []

Assuming there are no nested [], like [ac], you can try the following code.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Tekst", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Start", each Text.PositionOf([Tekst],"[",Occurrence.All)),
    #"Expanded Start" = Table.ExpandListColumn(#"Added Custom", "Start"),
    #"Added to Column" = Table.TransformColumns(#"Expanded Start", {{"Start", each if _ = null then null else List.Sum({_, 1}), type number}}),
    #"Added Custom1" = Table.AddColumn(#"Added to Column", "Length", each if [Start] = null then null else Text.PositionOf(Text.Middle([Tekst],[Start],Text.Length([Tekst])),"]")),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Extracted Text", each if [Start] = null then null else Text.Range([Tekst],[Start],[Length])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Start", "Length"})
in
    #"Removed Columns"
 
Upvote 0
Re: Extract text in between square pracits []

A different take where you end up with your findings in one row is this one:
Code:
let
    Source = #table({"Text"}, {{"blabla and [abc] and [defg] end"}, {"[sad]asd eert[re]re"}}),
    Prep = Table.AddColumn(Source, "Prep", each List.Skip(Text.Split([Text], "["), 1)),
    List = Table.AddColumn(Prep, "List", each List.Transform([Prep], each Text.Split(_, "]"){0})),
    Tbl = Table.AddColumn(List, "Table", each Table.FromRows({[List]})),
    Expand = Table.ExpandTableColumn(Tbl, "Table", Table.ColumnNames(Table.Combine(Tbl[Table]))),
    #"Removed Columns" = Table.RemoveColumns(Expand,{"Prep", "List"})
in
    #"Removed Columns"
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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