Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
I'm not good at regular expressions. I try to filter texts in bracket,

string temp="[Apple][Orange]Eating";

I need a array fill of Apple and Orange, how can i do that?


You can use the Regex.Split Method[^].

Use these links to find out how regular expressions work:
.NET Framework Regular Expressions[^]
Regular Expression Language Elements[^]
Share this answer
Simon Bang Terkildsen 25-Sep-11 11:20am    
My 5, I wasn't aware Regex had a split method
André Kraak 25-Sep-11 11:47am    
Me neither, where would we be without Google?

Sergey Alexandrovich Kryukov 25-Sep-11 15:31pm    
Good, my 5. I also did not pay attention there was Regex.Split. Great to know, thank you for sharing.
Simon Bang Terkildsen 25-Sep-11 15:45pm    
Yeah or just paying attension to the docs or intellisense :)
string inputString = "[Apple][Orange]Eating";

MatchCollection matches = Regex.Matches(inputString, @"\[(?<content>.+?)\]");

string[] theArray = matches.Cast<Match>()
                           .Select(m => m.Groups["content"].Value).ToArray();

You can use Regex.Split instead of Regex.Matches like André says.
Share this answer
Sergey Alexandrovich Kryukov 25-Sep-11 15:30pm    
Good, my 5.
Simon Bang Terkildsen 25-Sep-11 15:50pm    
Thank you, SA
emrea 26-Sep-11 17:31pm    
You saved my day TY:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900