regexp : get all the match for one expression
조회 수: 5 (최근 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 ?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio and Video Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!