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