regexp : get all the match for one expression

6 ビュー (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by