regexp : get all the match for one expression

5 views (last 30 days)
Hi, I have this variable :
var= 'bigFamily_middleFamily_littleFamily_childName'
% each parts of this name can inclus numbers
And I want to extract all the family name :
bigFamily_
bigFamily_middleFamily_
bigFamily_middleFamily_littleFamily_
But with my code :
clear all
clc
var= 'bigFamily_middleFamily_littleFamily_childName'
pattern_family=sprintf('([a-zA-Z0-9]+_)*')
var_family=regexp(var,pattern_family,'match')
I only get the longest family name :
var_family =
'bigFamily_middleFamily_littleFamily_'
The number of name can change (can 0,1,2,3 or more). Is it possile to get a vector of match string with different strings ?

Accepted Answer

Guillaume
Guillaume on 6 Mar 2015
It's not possible to do what you want with just a regular expression. A character can only be part of one match, so once you've captured 'bigFamily_' you can't capture it again as part of bigFamily_middleFamily_ within the same regular expression.
What I would do is just capture each *family part and rebuild all the possible combinations:
var = 'bigFamily_middleFamily_littleFamily_childName';
subfamilies = regexp(var, '[a-zA-Z0-9]+_', 'match');
combinations = num2cell(logical(tril(ones(numel(subfamilies)))), 2);
families = cellfun(@(c) strcat(subfamilies{c}), combinations, 'UniformOutput', false)
Or if you don't want the trailing '_':
var = 'bigFamily_middleFamily_littleFamily_childName';
subfamilies = regexp(var, '[a-zA-Z0-9]+(?=_)', 'match');
combinations = num2cell(logical(tril(ones(numel(subfamilies)))), 2);
families = cellfun(@(c) strjoin(subfamilies(c), '_'), combinations, 'UniformOutput', false)
  4 Comments
Ortinomax
Ortinomax on 6 Mar 2015
It's all I want, but the rest of my code is what it is. I don't have a good list of cells.
But with your first answer I build something I find good.
(And in this code, there is the strsplit function I don't have on this old Matlab.)
Thank for your help.
Guillaume
Guillaume on 6 Mar 2015
You can replace the strsplit(x, y) with regexp(x, y, 'split')

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!