Extract Specific Character Formats from a Text String

Excel2021

New Member
Joined
Mar 26, 2021
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to extract anything that comes after a hypen and has the following format character format "A123" from a Text String in power query (an excel formula would also work). Occasionally some of the formats I need to pull may vary and contain the format "AA123" or "AA123-AA".

String examples of each type are below in quotes and what I would like to pull is after the = on the right of the strings.

"GBS-1S-65-X555-7" = X555
"MRK-4S-95-XX555-7" = XX555
"KLS-5S-38-X555-UV" = X555-UV

Thanks in Advance.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
String examples of each type are below in quotes and what I would like to pull is after the = on the right of the strings.

"GBS-1S-65-X555-7" = X555
"MRK-4S-95-XX555-7" = XX555
"KLS-5S-38-X555-UV" = X555-UV
Sounds like
Power Query:
Text.AfterDelimiter(_, "=", {0, RelativePosition.FromEnd})
does the job.
 
Upvote 0
I tried the formula above but it did not seem to work for me. There is no "=" in the string. Only what is in the quotes would be an example of a typical string.

Thanks
 
Upvote 0
How about

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    result = Table.AddColumn(Source, "Custom", each
        let
            sp = Text.Split([Column1],"-"),
            txt = Text.AfterDelimiter([Column1], "-", {0 + Number.From(List.Count(sp) = 5), RelativePosition.FromEnd}),
            pos = Text.PositionOfAny(sp{List.Count(sp)-1},{"A".."Z"},Occurrence.All)
        in
            if not Text.Contains(txt,"-") or List.Count(pos) = 2 then txt else Text.Split(txt,"-"){0}
    )
in
    result

Book1
ABCD
1Column1Column1Custom
2GBS-1S-65-X555-7GBS-1S-65-X555-7X555
3MRK-4S-95-XX555-7MRK-4S-95-XX555-7XX555
4KLS-5S-38-X555-UVKLS-5S-38-X555-UVX555-UV
5KLS-5S-38-X555KLS-5S-38-X555X555
Sheet1
 
Upvote 0
How about

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    result = Table.AddColumn(Source, "Custom", each
        let
            sp = Text.Split([Column1],"-"),
            txt = Text.AfterDelimiter([Column1], "-", {0 + Number.From(List.Count(sp) = 5), RelativePosition.FromEnd}),
            pos = Text.PositionOfAny(sp{List.Count(sp)-1},{"A".."Z"},Occurrence.All)
        in
            if not Text.Contains(txt,"-") or List.Count(pos) = 2 then txt else Text.Split(txt,"-"){0}
    )
in
    result

Book1
ABCD
1Column1Column1Custom
2GBS-1S-65-X555-7GBS-1S-65-X555-7X555
3MRK-4S-95-XX555-7MRK-4S-95-XX555-7XX555
4KLS-5S-38-X555-UVKLS-5S-38-X555-UVX555-UV
5KLS-5S-38-X555KLS-5S-38-X555X555
Sheet1
This is great. It is definitely the closest I have been with this. On line 4 I just realized that sometimes the format also flips for some of the strings so it can also show up as KLS-5S-38-X555-UV or KLS-5S-38-UV-X555. Is possible to capture that also?

Thank you very much.
 
Upvote 0
Maybe with one extra line

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    result = Table.AddColumn(Source, "Custom", each
        let
            sp = Text.Split([Column1],"-"),
            txt = Text.AfterDelimiter([Column1], "-", {0 + Number.From(List.Count(sp) = 5), RelativePosition.FromEnd}),
            pos = Text.PositionOfAny(sp{List.Count(sp)-1},{"A".."Z"},Occurrence.All),
            pos2 = Text.PositionOfAny(sp{List.Count(sp)-2},{"A".."Z"},Occurrence.All)
        in
            if not Text.Contains(txt,"-") or List.Count(pos) = 2 or List.Count(pos2) = 2 then txt else Text.Split(txt,"-"){0}
    )
in
    result
 
Upvote 0
Maybe with one extra line

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    result = Table.AddColumn(Source, "Custom", each
        let
            sp = Text.Split([Column1],"-"),
            txt = Text.AfterDelimiter([Column1], "-", {0 + Number.From(List.Count(sp) = 5), RelativePosition.FromEnd}),
            pos = Text.PositionOfAny(sp{List.Count(sp)-1},{"A".."Z"},Occurrence.All),
            pos2 = Text.PositionOfAny(sp{List.Count(sp)-2},{"A".."Z"},Occurrence.All)
        in
            if not Text.Contains(txt,"-") or List.Count(pos) = 2 or List.Count(pos2) = 2 then txt else Text.Split(txt,"-"){0}
    )
in
    result
It doesn't seem to handle strings with "UV-" or "-UV" properly. If the string ends in "-UV" the rest of the string won't show up. Is there a way to split these from the string column and move them to their own column? That way your code could be applied to the leftover string and I can could concatenate the 2 UV columns back afterwards.

Thanks
 
Upvote 0
Can you give multiple examples with desired outcomes? Then I can look again, maybe tomorrow
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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