views:

63

answers:

4

i have text something like this.

@@MMIVLoader@[email protected]@BCM_7400S_LE@Product@Aug 21 2009@
@@MMIVLib@[email protected]@BCM_7400S_LE@Product@Aug 21 2009@
@@HuaweFGDLDrv@[email protected]@7324@PRODUCT@Aug 20 2009@
@@ProtectVer@[email protected] @BCM_SDE5.03@PRODUCT@Aug 4 2009 06:56:19@
@@KernelSw@[email protected]@BCM-7454@PRODUCT@ Dec 19 2007@
@@ReceiverSw@[email protected]@HWBC01ZS@PRODUCT@May 3 2010@

i want the out put in an array like

MMIVLoader 4.1.2
MMIVLib         4.1.2
HuaweFGDLDrv 01.00.09
ProtectVer 127.8.1 
KernelSw 0.0.1
ReceiverSw E.5.6.001

Can any one suggest me how to do this in c# using regular expression or is there a any sophisticated way to do this

thanks in advance

+7  A: 

This is easy, you can just split by @ (removing the empty items) and pull the first and third items.

var list = myString.Split(new String[] {Environment.NewLine},
          StringSplitOptions.RemoveEmptyEntries)                
   .Select(item => item.Split(new char[] {'@'}, 
          StringSplitOptions.RemoveEmptyEntries))
   .Where(a => a.Length > 2)
   .Select(a => new { Item = a[0], Version = a[2] }).ToArray();
ck
Beautiful. I was thinking about splitting it, but the extra @ annoyed me. `RmoveEmptyEntries` is a nice option.
Mark
how can we do this using regular expressions ?
I've updated so it will run on the entire string.
ck
@user340015 you're looking for problems? :-/
Stephane
A: 

For completeness, here is a LINQ version with query syntax :)

string[] lines = new string[] { 
            "@@MMIVLoader@[email protected]@BCM_7400S_LE@Product@Aug 21 2009@",
            "@@MMIVLib@[email protected]@BCM_7400S_LE@Product@Aug 21 2009@",
            "@@HuaweFGDLDrv@[email protected]@7324@PRODUCT@Aug 20 2009@",
            "@@ProtectVer@[email protected] @BCM_SDE5.03@PRODUCT@Aug  4 2009 06:56:19@",
            "@@KernelSw@[email protected]@BCM-7454@PRODUCT@ Dec 19 2007@",
            "@@ReceiverSw@[email protected]@HWBC01ZS@PRODUCT@May  3 2010@" };
var q = from components in
           (from line in lines
            select line.Split(new char[] { '@' }, 
                     StringSplitOptions.RemoveEmptyEntries))
        select new { Name = components[0], Version = components[2] };
foreach (var item in q)
{
    Console.WriteLine("Item: Name={0} Version={1}", item.Name, item.Version);
}
Isak Savo
You are aware mine is a Linq solution, right?
ck
@ck: yeah I know.. I meant the query syntax. I'll rephrase
Isak Savo
+1  A: 

If you do want a crazy regex solution, you can use this:

var matches = Regex.Matches(
    input,
    "@@(?<name>.*?)@(Product|Object)Ver@(?<ver>.*?)@",
    RegexOptions.IgnoreCase
).Cast<Match>().Select(m => m.Groups);

foreach (var match in matches)
{
    Console.WriteLine("{0} {1}", match["name"], match["ver"]);
}
Chris Schmich
+2  A: 

Or simply remove extra stuff from line

Regex.Replace(text, @"^@@([^@]+)@[^@]+@([^@]+).*", "$1,$2",RegexOptions.Multiline);

to get

MMIVLoader,4.1.2
MMIVLib,4.1.2
HuaweFGDLDrv,01.00.09
ProtectVer,127.8.1
KernelSw,0.0.1
ReceiverSw,E.5.6.001

And then, just split by comma on each line to get array

S.Mark