Method to GREEDILY select an optional text using regular expressions?

2 Ansichten (letzte 30 Tage)
Ana Alonso
Ana Alonso am 19 Nov. 2018
Bearbeitet: Stephen23 am 13 Sep. 2019
I'm trying to pick out the name of some folders in a directory, and each have slightly variable names. The names of the folders are:
UD_epoch
UD_Epoch
epoch
Epoch
UD_epoch1
UD_Epoch2
Right now the regular expression I'm using to pick out these folders is:
regexp(nfolders, '(UD.?(e|E)poch\d?)','match')
This picks out the first four folders successfully, but returns the last two as 'UD_epoch' & 'UD_Epoch.' How do I specify that I want the optional element to be included?
Thanks!

Antworten (1)

per isakson
per isakson am 13 Sep. 2019
Bearbeitet: per isakson am 13 Sep. 2019
Try
%%
nfolders = {
'UD_epoch'
'UD_Epoch'
'epoch'
'Epoch'
'UD_epoch1'
'UD_Epoch2'
};
%%
cac = regexp( nfolders, '^(UD_)?(e|E)poch\d?$', 'match' );
it returns
>> cac{:}
ans =
1×1 cell array
{'UD_epoch'}
ans =
1×1 cell array
{'UD_Epoch'}
ans =
1×1 cell array
{'epoch'}
ans =
1×1 cell array
{'Epoch'}
ans =
1×1 cell array
{'UD_epoch1'}
ans =
1×1 cell array
{'UD_Epoch2'}
>>
/R2018b
  2 Kommentare
Stephen23
Stephen23 am 13 Sep. 2019
Bearbeitet: Stephen23 am 13 Sep. 2019
+1 With the 'once' option makes the outputs easier to work with (no nested cell arrays):
>> regexp( nfolders, '(UD_)?[eE]poch\d*', 'match' ,'once')
ans =
'UD_epoch'
'UD_Epoch'
'epoch'
'Epoch'
'UD_epoch1'
'UD_Epoch2'

Melden Sie sich an, um zu kommentieren.

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!

Translated by