フィルターのクリア

Problem with regular expressions

1 回表示 (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2014 年 1 月 25 日
回答済み: the cyclist 2014 年 1 月 25 日
Given the string:
str='""A_3_1"": [""choice_0"", ""choice_1"", ""choice_2"", ""choice_3""], ""A_2_1"": [""choice_1"", ""choice_2""]'
I want to group with regexp from the numbers after the word choice for the for the two different situations A_3_1 and A_2_1.
The output for the A_3_1 will be:
[0 1 2 3]
and the output for the A_2_1 will be:
[1 2]

採用された回答

the cyclist
the cyclist 2014 年 1 月 25 日
Here is one way. It is not tremendously robust. For example, it assumes that the "choices" will always be single-digit numbers. However, it should at least give you a rudimentary algorithm that works, as a starting point.
% Identify locations of indicators. Appending the extra 'A' to get the end of the string
indices = regexp([str,'A'],'A');
numberIndices = numel(indices) - 1;
for ni = 1:numberIndices
% Find the substring for this index
substr = str(indices(ni):indices(ni+1)-1);
% Find the location of the beginning of the "choice" strings
choiceIdx = regexp(substr,'choice_');
% Find the locations of the digits following each choice, and convert to numeric.
% Each vector of values is stored in a cell array.
values{ni} = str2num(substr(choiceIdx+7)')'
end

その他の回答 (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