Create multiple sub arrays in one line!

조회 수: 12 (최근 30일)
Francisco Dias
Francisco Dias 2019년 11월 27일
댓글: Star Strider 2019년 11월 27일
Hi everyone, I have the string bellow as input to a function
'(44) (hello) (good) ()'
the function looks for indices positions of left and right parenthesis...
open = strfind(text, '(');
close = strfind(text, ')');
after that I want to isolate everything inside the parenthesis...
I was hoping there was a fast way of doing this something like:
output = text(open:close);
but it doesn't seem to work does anyone has a clue if this is possible?!

채택된 답변

Star Strider
Star Strider 2019년 11월 27일
Try this:
str = '(44) (hello) (good) ()';
out = regexp(str, '(?<=\()\w*(?=\))', 'match')
producing:
out =
1×3 cell array
{'44'} {'hello'} {'good'}
See the documentation on regexp (and its friends) for details.
Make appripriate changes to get the result you wannt.
  댓글 수: 4
Francisco Dias
Francisco Dias 2019년 11월 27일
Woow this is great! thank you ;)
Star Strider
Star Strider 2019년 11월 27일
As always, my pleasure!

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

추가 답변 (1개)

Turlough Hughes
Turlough Hughes 2019년 11월 27일
편집: Turlough Hughes 2019년 11월 27일
This is one way to go about it:
a=strsplit(text(text~=' '),{'(',')'})
output=sprintf('%s',a{:})
  댓글 수: 1
Turlough Hughes
Turlough Hughes 2019년 11월 27일
편집: Turlough Hughes 2019년 11월 27일
Actually, just realising you could also write:
output=text(text~=' ' | text~='(' | text~=')')

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

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by