views:

141

answers:

4

I have a string like A150[ff;1];A160;A100;D10;B10'

in which I want to extract A150, D10, B10

In between these valid string, i can have any characters. The one part that is consistent is the semicolumn between each legitimate strings.

Again the junk character that I am trying to remove itself can contain the semi column

A: 

First,you should use string s="A150[ff;1];A160;A100;D10;B1"; s.IndexOf("A160"); Through this command you can get the index of A160 and other words. And then s.Remove(index,count).

+3  A: 

Without having more detail for the specific rules, it looks like you want to use String.Split(';') and then construct a regex to parse out the string you really need foreach string in your newly created collection. Since you said that the semi colon can appear in the "junk" it's irrelevant since it won't match your regex.

nithins
You beat me too it.
monksy
+1  A: 
        var input = "A150[ff+1];A160;A150[ff-1]";
        var temp = new List<string>();
        foreach (var s in input.Split(';'))
        {
            temp.Add(Regex.Replace(s, "(A[0-9]*)\\[*.*", "$1"));
        }
        foreach (var s1 in temp.Distinct())
        {
            Console.WriteLine(s1);   
        }

produces the output

A150
A160
Phillip Ngan
A: 

If you only want to remove the 'junk' inbetween the '[' and ']' characters you can use regex for that

   Regex regex = new Regex(@"\[([^\}]+)\]");

   string result = regex.Replace("A150[ff;1];A160;A100;D10;B10", "");

Then String.Split to get the individual items

Matt