Click here to Skip to main content
16,016,227 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i want how to split this string

C#
string s = "[1] - blah blah [2] - [bbbbbb]";
string[] words = Regex.Split(s, "??????");

in Regex.Split how to use ?
i want this output
1
2
bbbbbb
Posted
Updated 2-Sep-13 22:43pm
v2

I'm sure somebody with more Regular Expression experience could do better. That said, try this...
C#
string s = "[1] - blah blah [2] - [bbbbbb]";

System.Text.RegularExpressions.MatchCollection matches = Regex.Matches(s, @"\[(\w*)\]");

foreach (System.Text.RegularExpressions.Match match in matches)
{
    // the item at index 1 is your value without the brackets, the value at index 0 is the value including the brackets wrapping it.
    MessageBox.Show(match.Groups[1].Value);
}
 
Share this answer
 
Comments
Menon Santosh 3-Sep-13 2:01am    
Good Job my +5
idenizeni 3-Sep-13 2:06am    
Thanks :)
Punamchand Dhuppad 3-Sep-13 2:09am    
nice one
Joezer BH 3-Sep-13 2:24am    
5ed!
KururuLABO 3-Sep-13 3:15am    
How i can get "match.Groups[1].Value" to varible ?
int i = 1;
int s= 2;
string = "bbbbbb";
Hi Instead of split you can use Matches and take the values using foreach loop

Hope this helps you.
C#
string pattern = @"\[(.*?)\]";
string str = "[1] - blah blah [2] - [bbbbbb]";
string matches = Regex.Matches(str, pattern);
string value = string.Empty;
foreach (Match m in matches)
{
     value += m.Groups[1].ToString()+",";
}
 
Share this answer
 

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