Read data from string

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일

1 개 추천

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

This will not work if
x='abc123(xyz456)ab(cde)';
Evan
Evan 2013년 8월 7일
편집: Evan 2013년 8월 7일
True. It assumes only one set of parenthesis. I used the character identifier in case of spaces, but that might not be a needed flexibility. Regardless, I suppose a better version to account for that would be similar to yours:
regexp(x,'(?<=\()[\w\s]+(?=\))','match')
or
x='abc123(xyz456)aaa(123)mmmm';
regexp(x,'(?<=\().+?(?=\))','match')
where the added ? stands for
Lazy expression: match as few characters as necessary.
Cedric
Cedric 2013년 8월 7일
편집: Cedric 2013년 8월 7일
My preference goes to Per lazy expr., but I wanted to mention the following, counter-intuitive behavior:
>> tok = regexp('_A_A-', '_(.*?)-', 'tokens')
tok =
{1x1 cell}
>> tok{1}
ans =
'A_A'
and not just A, as many people would expect.
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일

1 개 추천

y=regexp(x,'(?<=\()[\w]+(?=\))','match')

댓글 수: 1

%or
x=x='abc123 (xyz 45_6) ddd (rtr)ccc'
y=regexp(x,'\(([\w\s]+)\)','tokens');
celldisp(y)

댓글을 달려면 로그인하십시오.

Jan
Jan 2013년 8월 7일

1 개 추천

x = 'abc123(xyz456)';
ini = strfind(x, '(');
fin = strfind(x, ')');
key = x(ini(1) + 1:fin(1) - 1);

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2013년 8월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by