Read data from string

1 回表示 (過去 30 日間)
Artyom
Artyom 2013 年 8 月 7 日
I have string line:
x='abc123(xyz456)'
How to read information only in brackets, to have result:
y='xyz456'.

採用された回答

Evan
Evan 2013 年 8 月 7 日
編集済み: Evan 2013 年 8 月 7 日
>> 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 件のコメント
per isakson
per isakson 2013 年 8 月 7 日
編集済み: per isakson 2013 年 8 月 7 日
Surprise!
regexp('_A_A-', '(?<=_)[^_]+?(?=-)', 'match' )
ans =
'A'
Thus, doc should say something like
Lazy expression: match as few characters as necessary **downstream**.
Cedric
Cedric 2013 年 8 月 7 日
編集済み: Cedric 2013 年 8 月 8 日
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!

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 7 日
y=regexp(x,'(?<=\()[\w]+(?=\))','match')
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 7 日
%or
x=x='abc123 (xyz 45_6) ddd (rtr)ccc'
y=regexp(x,'\(([\w\s]+)\)','tokens');
celldisp(y)

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


Jan
Jan 2013 年 8 月 7 日
x = 'abc123(xyz456)';
ini = strfind(x, '(');
fin = strfind(x, ')');
key = x(ini(1) + 1:fin(1) - 1);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by