Manipulate strings in a table- remove leading numbers if present

12 ビュー (過去 30 日間)
Marcus Glover
Marcus Glover 2023 年 3 月 8 日
コメント済み: Voss 2023 年 3 月 9 日
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' }
  2 件のコメント
Marcus Glover
Marcus Glover 2023 年 3 月 9 日
Perfect, thanks so much!!
Voss
Voss 2023 年 3 月 9 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by