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

1 visualización (últimos 30 días)

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

Respuesta aceptada

Matthew Eicholtz
Matthew Eicholtz el 14 de Oct. de 2016
Editada: Matthew Eicholtz el 14 de Oct. de 2016
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);

Más respuestas (2)

michio
michio el 14 de Oct. de 2016
Editada: michio el 14 de Oct. de 2016
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 el 14 de Oct. de 2016
Use
inds1 = strfind(A.input,']');
inds2 = strfind(A.input,'[');
to get the locations of the variables.

Community Treasure Hunt

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

Start Hunting!

Translated by