Regular Expression Problem , Please Help

조회 수: 3 (최근 30일)
Yowh
Yowh 2012년 9월 27일
I have no idea how to do this ..
str = '[a,b,c,d,e,[f1,f2],g],[u,v,w,[x1,x2],[y1,y2],z]';
Using this code:
regexp(str,'\[.*?(\[.*?\],[.*?\]).*?\]','tokens');
i got an answer like this:
ans =[f1,f2],g],[u,v,w,[x1,x2]
but what I want is like this:
ans = [x1,x2],[y1,y2]
  댓글 수: 3
Yowh
Yowh 2012년 9월 27일
the string is dynamic ..
Matt Fig
Matt Fig 2012년 9월 28일
That doesn't really answer the question. I showed you how to find the answer you requested, then I asked you to give the general form, including the rule for finding what you want. "The string is dynamic" tells me nothing really. Good luck.

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

답변 (1개)

Walter Roberson
Walter Roberson 2012년 9월 27일
Remember, .* will match everything including [ or ] characters. You need to exclude those characters from the search if you are looking for the ] matching a [ .
  댓글 수: 1
Yowh
Yowh 2012년 9월 28일
I have tried this but it doesn't work:
ans = regexp(str,'\[.*?\[[^\]]*?\].*?\]','match')

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by