Effacer les filtres
Effacer les filtres

Read data from string

1 vue (au cours des 30 derniers jours)
Artyom
Artyom le 7 Août 2013
I have string line:
x='abc123(xyz456)'
How to read information only in brackets, to have result:
y='xyz456'.

Réponse acceptée

Evan
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
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
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!

Connectez-vous pour commenter.

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Août 2013
y=regexp(x,'(?<=\()[\w]+(?=\))','match')
  1 commentaire
Azzi Abdelmalek
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)

Connectez-vous pour commenter.


Jan
Jan le 7 Août 2013
x = 'abc123(xyz456)';
ini = strfind(x, '(');
fin = strfind(x, ')');
key = x(ini(1) + 1:fin(1) - 1);

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by