Click here to Skip to main content
15,037,009 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);
}
   
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";
idenizeni 3-Sep-13 3:24am
   
Like any other variable...
e.g.
string x = match.Groups[1].Value;
...
int i = -1;
int.TryParse(match.Groups[1].Value, out i);

Is this what you meant?
KururuLABO 3-Sep-13 4:24am
   
I want to send value in match.groups[1].value to varible
I try you code MessageBox will show 1 2 bbbbb
i want you split text to varible
Example
[1] - blah blah [2] - [bbbbbb]
int = 1;
int = 2;
string = "bbbbbb";

Split text to varible :)
KururuLABO 3-Sep-13 4:28am
   
If you don't what i mean see this picture
http://i1259.photobucket.com/albums/ii547/kururulabo1/Mean.png
idenizeni 3-Sep-13 14:14pm
   
Will there always be 3 items or can the number of items change each time? If the number can change then I don't understand why you want to assign to variables as you wouldn't know how many variables to declare in your code. This sounds like a job for a list/array. In your original post you were attempting to split the values into a string array, if you want to put just the values into a list of strings you could do this...

//
string s = "[1] - blah blah [2] - [bbbbbb]";
System.Text.RegularExpressions.MatchCollection matches = Regex.Matches(s, @"\[(\w*)\]");

List<string> words = (from System.Text.RegularExpressions.Match m in matches select m.Groups[1].Value).ToList();

foreach (string word in words)
{
MessageBox.Show(word);
}
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()+",";
}
   

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