regexp : get all the match for one expression

6 次查看(过去 30 天)
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 ?

采纳的回答

Guillaume
Guillaume 2015-3-6
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 个评论
Ortinomax
Ortinomax 2015-3-6
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 2015-3-6
You can replace the strsplit(x, y) with regexp(x, y, 'split')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by