Manipulate strings in a table- remove leading numbers if present

13 views (last 30 days)
I have strings in a table that I would like to strip the leading 'numerical group' of characters from if they are present- and they are not always present. The 'numerical group' does not have to be a real number- 12.345.6789 is possible, but it will always be made up of numbers and decimal points.
The 'numerical group' may come in many possible length permutations, so counting number of places will not work. The string I want to keep may also have numbers and decimal places in it, but I only want to remove this first 'group' of numbers. Described this way, it seems like a regular expression may get the job done.
If numbers do appear at first, there will always be a space between them and the rest of the string I want to keep. There may be more spaces, I would only want to eliminate everything before the first space. Described that way, an 'IF number/decimal THEN remove all before first space delimiter' loop may work.
Or there may be (probably...) some simpler solution here. I am struggling with handling it within a table as much as I am the regular expression or loop part.
The strings are currently in a categorical table variable, and while I doubt I can do this without converting them, I'd like them to end up that way.
names={'1 HEAD 123';'1.10 BODY123 456';'1.5 BODY/HEAD';'22.456 BODY';'BODY123';'1.11.123.1234 HEAD 123'};
T=table(names);
T.names=categorical(T.names)
T = 6×1 table
names ______________________ 1 HEAD 123 1.10 BODY123 456 1.5 BODY/HEAD 22.456 BODY BODY123 1.11.123.1234 HEAD 123
solution={'HEAD 123';'BODY123 456';'BODY/HEAD';'BODY';'BODY123';'HEAD 123'};
S=table(solution);
S.solution=categorical(S.solution)
S = 6×1 table
solution ___________ HEAD 123 BODY123 456 BODY/HEAD BODY BODY123 HEAD 123

Accepted Answer

Voss
Voss on 8 Mar 2023
load S
solution = regexprep(names,'^[\d\.]+\s+','')
solution = 6×1 cell array
{'HEAD 123' } {'BODY123 456'} {'BODY/HEAD' } {'BODY' } {'BODY123' } {'HEAD 123' }

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!