Condense code in Split string operation
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sanju Das
am 13 Sep. 2022
Bearbeitet: Sanju Das
am 14 Sep. 2022
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
0 Kommentare
Akzeptierte Antwort
Stephen23
am 13 Sep. 2022
s = "xxx_yyy_zzz.xlsx";
regexp(s,'[^_.]+(?=\.)','match','once')
1 Kommentar
Weitere Antworten (1)
Les Beckham
am 13 Sep. 2022
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"
extractBetween(s, '_', '_')
If it is a char vector it will take two lines
s = 'xxx_yyy_zzz.xlsx'
s1 = extractBetween(s, '_', '_')
s2 = s1{:}
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!