Manipulate strings in a table- remove leading numbers if present

7 次查看(过去 30 天)
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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by