How to extract a string before a number?

19 Ansichten (letzte 30 Tage)
Meenal
Meenal am 30 Aug. 2022
Bearbeitet: Stephen23 am 20 Mär. 2023
Hello everyone, I need to know how to extract a substring from strings given below such that as soon as it encounters a digit, it returns the substring before that.
e.g.
str = 'abcd-xyzw-1.2.3.zip'
str2 = 'abcd_xyzw_2.3.1.zip'
it should then return the substring as
sub_str = 'abcd-xyzw-'
sub_str2 = 'abcd_xyzw_'
How can it be done?
Thanks in advance

Akzeptierte Antwort

Chunru
Chunru am 30 Aug. 2022
str = 'abcd-xyzw-1.2.3.zip';
str2 = 'abcd_xyzw_2.3.1.zip';
idx = regexp(str, '\d');
sub_str = str(1:idx(1)-1)
sub_str = 'abcd-xyzw-'
idx = regexp(str2, '\d');
sub_str2 = str2(1:idx(1)-1)
sub_str2 = 'abcd_xyzw_'

Weitere Antworten (2)

Karim
Karim am 30 Aug. 2022
You can use the regexp function to find the indexes for the numbers. Then you can split the char array using that index (minus one so that you do not have that first number)
% define the string, note i used a char array as in the OP's question
str = 'abcd-xyzw-1.2.3.zip';
% use the regexp function to obtain the indexes of the numbers
[~,idx] = regexp(str,'(?<!\d)(\d)+(?!\d)','match');
% get the sub string by taking the first characters (minus 1 so that we
% don't copy the first number)
sub_str = str( 1:(idx(1)-1) )
substr = 'abcd-xyzw-'

Stephen23
Stephen23 am 30 Aug. 2022
Bearbeitet: Stephen23 am 20 Mär. 2023
The simple approach using REGEXP, without wasting time fiddling around with indices:
str = 'abcd-xyzw-1.2.3.zip';
sub = regexp(str,'^\D+','match','once')
sub = 'abcd-xyzw-'
This approach also directly works with multiple strings at once (unlike using indexing):
str = {'abcd-xyzw-1.2.3.zip';'abcd_xyzw_2.3.1.zip'};
sub = regexp(str,'^\D+','match','once')
sub = 2×1 cell array
{'abcd-xyzw-'} {'abcd_xyzw_'}

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by