Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a string like this::
";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;"


I need to get back
abc def xyz

i.e which is in between ; and :

I am only able to get back abc..but not def and xyz.. I tried this::
C#
string abc = ";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";
            
                int firstpos = abc.IndexOf(';');
                int secondpos = abc.IndexOf(':');

                string result = abc.Substring(firstpos + 1, secondpos - 1);
Posted

Try something like this:
C#
string abc = ";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";

Regex pattern = new Regex(";(?<val>[^;:]+?):", RegexOptions.ExplicitCapture);

IEnumerable<string> values = pattern.Matches(abc).Cast<Match>().Select(m => m.Groups["val"].Value);
 
Share this answer
 
v2
Comments
JaideepChandra 4-Aug-14 12:31pm    
Thank you so much..That works fine.
Sergey Alexandrovich Kryukov 4-Aug-14 13:26pm    
This will work, of course, my 5, but the question suggests that the whole approach is wrong. Please see my answer.
—SA
"I need to get back" simply means that you are using wrong approach: using strings representing data instead of data itself. You should pass everywhere your original data structure, not a string representing this data structure. Well, if you pass this structure between hosts, you may need to use serialization of the data, but in this case learn and use serialization (which can be string or binary).

—SA
 
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