Read data from string

5 views (last 30 days)
Artyom on 7 Aug 2013
I have string line:
How to read information only in brackets, to have result:

Accepted Answer

Evan on 7 Aug 2013
Edited: Evan on 7 Aug 2013
>> x='abc123(xyz456)';
>> regexp(x,'(?<=\().+(?=\))','match')
ans =
This command uses regexp and, specifically, lookaround assertions. It's basically saying, if you find a group of characters, look behind to see if there is an "open parenthesis" character and look ahead to see if there is a "close parenthesis" character. If so, return all the characters between them.
Cedric Wannaz
Cedric Wannaz on 7 Aug 2013
Edited: Cedric Wannaz on 8 Aug 2013
Yep, in other words, it stops when it matches the last part of the pattern for the first time (lazy), but it doesn't pull back the starting point (the tail? ;-)) to minimize the match (not that lazy finally, or really really lazy in fact). Thankfully, you see/understand this once and you never forget it!

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 7 Aug 2013
  1 Comment
Azzi Abdelmalek
Azzi Abdelmalek on 7 Aug 2013
x=x='abc123 (xyz 45_6) ddd (rtr)ccc'

Sign in to comment.

Jan on 7 Aug 2013
x = 'abc123(xyz456)';
ini = strfind(x, '(');
fin = strfind(x, ')');
key = x(ini(1) + 1:fin(1) - 1);


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by