Try to understand a regexp example
Afficher commentaires plus anciens
regexp is a function by far I have used the most to work on the string-related Cody problems, which I have spent a lot of time trying to understand how it works and yet a lot of questions still remain. There is an example in doc regexp under Dynamic Regular Expressions (modified below)
a = 'zzabcbagh';
regexp(a, '(.{2,}).?(??@fliplr($1))','match','once');
This is to "find palindromes that are at least four characters long". What I don't understand here is why the two '.' (dots) are necessary. How about the first '?'
Besides, when 'once' is used, it returns as char; however, when it is igored ('all' by default), it returns as cell. Why?
Thank you in advance.
1 commentaire
Walter Roberson
le 5 Août 2019
When once is not used, then the pattern is to be applied repeatedly, finding all of the matches in the string. Suppose that only one match were found, then you propose that a character vector be returned, and that a cell be used if there were multiple matches. Okay then, now you as a programmer who does not know ahead of time how many matches there are must interpret the result. If there just happened to be one match, return would be char, you propose. So then for example
S = char(randi(['a' 'z'], 1, 10);
R = regexp(S, '[bp]', 'match') ;
How do you process R under your proposal? There could be 0, 1, or even up to 10 matches. So ahead of time you do not know if there is going to be exactly one match, so you cannot presume that R is char, and to process you would need to do
if ischar(R)
%exactly one match
Use R as a character vector
elseif isempty(R)
%empty cell
...
else
%R is a cell of results
end
Compare that to the current implementation: R is always a cell unless you told it 'once' and you do not need to test specially.
There is one call that returns character vector on one match and returns a cell on multiple matches: namely uigetfile with multiselect turned on. It is a nuisance that they special cased the single file possibility and people often get the call wrong.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!