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 Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by