Click here to Skip to main content
14,828,868 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey All,
I have a textbox.
In that some entries are made like
textbox.text = Tendulkar,Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)

I want an array of string which will have values like (srt,msd,viru,tdsc).
I guess we will first have to go around splitting by (';') and get a result array set of this.
and then split with ('(') and (')').
A for loop inside another is what i guess.

How can i get this??
Appreciate if u cud help me out.

Thanks
Akshay
Posted
Comments
[no name] 20-Oct-13 8:46am
   
You described what you have to do. Just do it....(N.B it is not a "for" inside a "for", like you described it: First you separate by ";" and afterwards you analyze the separated strings for "(...)").

But try to also to think forward. E.g. What should happen if somebody enters "Tendulkar(xyz),Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)"


...first have a look to OriginalGriff's answer ;)

1 solution

Easier way is to use a Regex:
C#
string s = "Tendulkar,Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)";
Regex reg = new Regex(@"(?<=\().+?(?=\))");
MatchCollection matches = reg.Matches(s);
List<string> separated = new List<string>();
foreach (Match m in matches)
    {
    separated.Add(m.Value);
    }
   
Comments
aks.shan 20-Oct-13 8:55am
   
Hi Original Griff,
This works wonders.
Would appreciate if u could help me in understanding regex.
Thanks.
Akshay
OriginalGriff 20-Oct-13 9:26am
   
There are whole books on Regular expressions!
This one is good:
http://www.amazon.co.uk/Regular-Expressions-Cookbook-ebook/dp/B008Y4OP1O/ref=sr_1_2?ie=UTF8&qid=1382275394&sr=8-2&keywords=regular+expressions
As is this:
http://www.amazon.co.uk/Mastering-Regular-Expressions-ebook/dp/B007I8S1X0/ref=sr_1_6?ie=UTF8&qid=1382275394&sr=8-6&keywords=regular+expressions
But look at Google and you'll find loads of basic tutorials which will explain them better than I can in a small textbox...
This is worth downloading as well:
http://www.ultrapico.com/Expresso.htm
Expresso - it's free, and it examines and generates Regular expressions.
aks.shan 20-Oct-13 10:17am
   
Thanks :)
OriginalGriff 20-Oct-13 10:28am
   
You're welcome!

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