How do seperate a string in different strings while not creating new strings for variables

조회 수: 1 (최근 30일)

I want to seperate a string but i dont know yet what is in the string. The only two thing I know is that if a name has a variable who look like:([XXXX]). I want to create a new string after the variable. Some variables however do not have a variable so I want to create a new string immediately after the name. Every whole string begins with a # which I dont want in my first new strings.

Deleting the # is not a problem

I tried finding the first letter of every word and checking if it is a letter with isletter. The first number shows if a word begins with a letter or something else like a bracket but after that I got stuck.

Example A.Input = '# Donald-Duck [Father] Heuy [Son] Goofy Dewey [Son] Daisy-Duck Scrooge-McDuck [Uncle]'

Wanted result: A.Output =

'Donald-Duck [Father]'

'Heuy [Son]'

'Goofy'

'Dewey [Son]'

'Daisy-Duck'

'Scrooge-McDuck [Uncle]'

If somebody could tell me how to solve this i would appreciate it

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2016년 10월 14일
편집: Matthew Eicholtz 2016년 10월 14일
For diversity of answers, here is another option (although I do not claim it is better than other answers!):
str = '# Donald-Duck [Father] Heuy [Son] Goofy Dewey [Son] Daisy-Duck Scrooge-McDuck [Uncle]';
% Get all the names, ignore the #
names = regexp(str,'\s','split');
names = names(2:end);
% Get index into output cell array for each name
ind = cumsum(cellfun('isempty',regexp(names,'[')));
% Make output
y = arrayfun(@(y) strjoin(names(ind==y),' '),1:max(ind),'uni',0);

추가 답변 (2개)

michio
michio 2016년 10월 14일
편집: michio 2016년 10월 14일
Using regular expression,,
input = '# Donald-Duck [Father] Heuy [Son] Goofy Dewey [Son] Daisy-Duck Scrooge-McDuck [Uncle]';
% To find location where "space + alphabet" occurs
expression = '\s\w';
% start index of the token
index = regexp(input,expression,'start');
% Let's insert random symbol
input(index) = '*';
% Split the input srting at the symbol "*"
expression = '*';
splitStr = regexp(input,expression,'split')

Marc Jakobi
Marc Jakobi 2016년 10월 14일
Use
inds1 = strfind(A.input,']');
inds2 = strfind(A.input,'[');
to get the locations of the variables.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by