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 日

1 投票

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 日
I actually liked this approach, but I wanted people to be able to set their delimiters.
for example:
split_between('[hello] [banana] [56]', '[', ']');
so I went with @Turlough answer.
Star Strider
Star Strider 2019 年 11 月 27 日
Note that both answers require that the delimiters be specified. Neither will work with unspecified delimiters.
Example —
split_between = @(str,delim1,delim2) regexpi(str, sprintf('(?<=\\%s)\\w*(?=\\%s)',delim1,delim2), 'match'); % Create Anonymous Function
out = split_between('[hello] [banana] [56]', '[', ']')
producing:
out =
1×3 cell array
{'hello'} {'banana'} {'56'}
as requested.
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 日

0 投票

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~=')')

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索

製品

リリース

R2019b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by