regexp : get all the match for one expression

조회 수: 5 (최근 30일)
Ortinomax
Ortinomax 2015년 3월 6일
댓글: Guillaume 2015년 3월 6일
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에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by