Read data from string
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have string line:
x='abc123(xyz456)'
How to read information only in brackets, to have result:
y='xyz456'.
0 commentaires
Réponse acceptée
Evan
le 7 Août 2013
Modifié(e) : Evan
le 7 Août 2013
>> x='abc123(xyz456)';
>> regexp(x,'(?<=\().+(?=\))','match')
ans =
'xyz456'
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.
6 commentaires
per isakson
le 7 Août 2013
Modifié(e) : per isakson
le 7 Août 2013
Surprise!
regexp('_A_A-', '(?<=_)[^_]+?(?=-)', 'match' )
ans =
'A'
Thus, doc should say something like
Lazy expression: match as few characters as necessary **downstream**.
Cedric
le 7 Août 2013
Modifié(e) : Cedric
le 8 Août 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!
Plus de réponses (2)
Azzi Abdelmalek
le 7 Août 2013
y=regexp(x,'(?<=\()[\w]+(?=\))','match')
1 commentaire
Azzi Abdelmalek
le 7 Août 2013
%or
x=x='abc123 (xyz 45_6) ddd (rtr)ccc'
y=regexp(x,'\(([\w\s]+)\)','tokens');
celldisp(y)
Jan
le 7 Août 2013
x = 'abc123(xyz456)';
ini = strfind(x, '(');
fin = strfind(x, ')');
key = x(ini(1) + 1:fin(1) - 1);
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!