i have text something like this.
@@MMIVLoader@ProductVer@4.1.2@BCM_7400S_LE@Product@Aug 21 2009@
@@MMIVLib@ObjectVer@4.1.2@BCM_7400S_LE@Product@Aug 21 2009@
@@HuaweFGDLDrv@ObjectVer@01.00.09@7324@PRODUCT@Aug 20 2009@
@@ProtectVer@ObjectVer@127.8.1 @BCM_SDE5.03@PRODUCT@Aug 4 2009 06:56:19@
@@KernelSw@ObjectVer@0.0.1@BCM-7454@PRODUCT@ Dec 19 2007@
@@ReceiverSw@ObjectVer@E.5.6.001@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
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();
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
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"]);
}
For completeness, here is a LINQ version with query syntax :)
string[] lines = new string[] {
"@@MMIVLoader@ProductVer@4.1.2@BCM_7400S_LE@Product@Aug 21 2009@",
"@@MMIVLib@ObjectVer@4.1.2@BCM_7400S_LE@Product@Aug 21 2009@",
"@@HuaweFGDLDrv@ObjectVer@01.00.09@7324@PRODUCT@Aug 20 2009@",
"@@ProtectVer@ObjectVer@127.8.1 @BCM_SDE5.03@PRODUCT@Aug 4 2009 06:56:19@",
"@@KernelSw@ObjectVer@0.0.1@BCM-7454@PRODUCT@ Dec 19 2007@",
"@@ReceiverSw@ObjectVer@E.5.6.001@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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.