Condense code in Split string operation

3 ビュー (過去 30 日間)
Sanju Das
Sanju Das 2022 年 9 月 13 日
編集済み: Sanju Das 2022 年 9 月 14 日
I have a excel file with the name 'xxx_yyy_zzz.xlsx'
Here, I want to extract only the part 'zzz' and for that I am using the following code
name = 'xxx_yyy_zzz.xlsx';
dummy1 = split('xxx_yyy_zzz.xlsx', '.');
dummy2 = split(dummy1{1}, '_');
final = dummy2{end};
My question is -- is it posible to condense these four lines into a single line of code?
Thanks
SD

採用された回答

Stephen23
Stephen23 2022 年 9 月 13 日
s = "xxx_yyy_zzz.xlsx";
regexp(s,'[^_.]+(?=\.)','match','once')
ans = "zzz"
  1 件のコメント
Sanju Das
Sanju Das 2022 年 9 月 14 日
編集済み: Sanju Das 2022 年 9 月 14 日
It works, thanks for the help

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

その他の回答 (1 件)

Les Beckham
Les Beckham 2022 年 9 月 13 日
If you put the filename in a string instead of a char vector you can do it in one line
s = "xxx_yyy_zzz.xlsx"
s = "xxx_yyy_zzz.xlsx"
extractBetween(s, '_', '_')
ans = "yyy"
If it is a char vector it will take two lines
s = 'xxx_yyy_zzz.xlsx'
s = 'xxx_yyy_zzz.xlsx'
s1 = extractBetween(s, '_', '_')
s1 = 1×1 cell array
{'yyy'}
s2 = s1{:}
s2 = 'yyy'

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by