Separate string with tag_string

조회 수: 1 (최근 30일)
galaxy
galaxy 2020년 8월 5일
댓글: Stephen23 2020년 8월 5일
Dear bros,
I want to separate long string which contains tags to each classify.
Example:
string = 'tag_A(this is tag A, it contains tag_B(this is tag_B, it contains tag_C(figure.png). This is still tag B). This is still tag A)';
output is array string or cell array such as:
this is tag A, it contains tag_B tag_A
this is tag_B, it contains tag_C tag_B
figure.png tag_C
This is still tag tag_B
This is still tag A tag_A
I used strfind, regexp, etc. but it does not work.
Do any one can help me?
Thank you so much

채택된 답변

Stephen23
Stephen23 2020년 8월 5일
편집: Stephen23 2020년 8월 5일
This should get you started. Adjust as required.
str = 'tag_A(this is tag A, it contains tag_B(this is tag_B, it contains tag_C(figure.png). This is still tag B). This is still tag A)';
abc = cell(0,2);
xyz = cell(0,2);
rgx = '(\w+)\((.+)\)';
tkn = regexp(str,rgx,'once','tokens');
while numel(tkn)
tag = tkn{1};
[tkn,spl] = regexp(tkn{2},rgx,'once','tokens','split');
abc = [abc;spl(1),tag]; %#ok<AGROW>
xyz = [spl(end),tag;xyz]; %#ok<AGROW>
end
out = [abc;xyz(2:end,:)]
giving:
out =
'this is tag A, it contains ' 'tag_A'
'this is tag_B, it contains ' 'tag_B'
'figure.png' 'tag_C'
'. This is still tag B' 'tag_B'
'. This is still tag A' 'tag_A'
Note that regular expressions cannot easily match arbitrarily-nested pairs of parentheses. This answer will only work for input strings with nested parentheses, i.e. (...(...)...), as your example shows. It will not work for multiple parenthesis pairs on the same level of nesting, i.e. (...)(...).
  댓글 수: 2
galaxy
galaxy 2020년 8월 5일
편집: galaxy 2020년 8월 5일
Dear Stephen Cobeldick,
Thank you for your answer.
It is very usefull with me.
However, can you help me for both cases: (...(...)...) and multiple parenthesis pairs on the same level of nesting (...)(...). When I ask this question, I did not think about that cases.
example:
str = 'tag_A(this is tag A, it contains tag_B(this is tag_B, it contains tag_C(figure.png), tag_C(figure1.png). This is still tag B). This is still tag A)';
Thank you
Stephen23
Stephen23 2020년 8월 5일
@galaxy: please show the expected output for your new example.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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