Regular Expression Problem , Please Help

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 commentaires

Matt Fig
Matt Fig le 27 Sep 2012
Modifié(e) : Matt Fig le 27 Sep 2012
What is the general form of the string? Will x1 and y1 be numbers? What about a, b, f1, g, u and z? I ask for the general form because right now it is not very interesting for regexp.
regexp(str,'\[x1.*\y2]','match')
Yowh
Yowh le 27 Sep 2012
the string is dynamic ..
Matt Fig
Matt Fig le 28 Sep 2012
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.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 27 Sep 2012

0 votes

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 commentaire

Yowh
Yowh le 28 Sep 2012
I have tried this but it doesn't work:
ans = regexp(str,'\[.*?\[[^\]]*?\].*?\]','match')

Connectez-vous pour commenter.

Question posée :

le 27 Sep 2012

Community Treasure Hunt

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

Start Hunting!

Translated by