• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Regex to pull specific pieces of data out of a string array

I need an expression that searches for specific strings and returns the value associated with that string if they exist.
For example I have a file which has the following lines in it:

system-serial-number:70000081
partner-system-id:0151698606
partner-system-name:frehfas82b
Controller-address:A
number-of-processors:4
memory-size:16376

I need to code an expression that searches for "number-of-processors:" and returns the value after the ":"
I also need an expression that searches for "partner-system-name:" and if it exists (this may not always be present in the data) return the value after the ":"

Any help would be much appreciated!
0
newimagent
Asked:
newimagent
  • 8
  • 5
1 Solution
 
nepaluzCommented:
Is the solution you want STRICTLY regex or any solution will do?
0
 
newimagentAuthor Commented:
What ive been trying to code has been regex, but any solution will do if it fits. Basically up to this point I have been able to use static pointers to the data I want:


Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
Dim systemID As String = slotConfigData(6).Substring(12)
Dim systemModel As String = slotConfigData(8).Substring(15)
Dim systemSerialNumber As String = slotConfigData(14).Substring(23)
'Dim cpuCount As String = slotConfigData(30).Substring(23)
Dim cpuCount As String= (Regex.Match(slotConfigData, (

Open in new window

0
 
nepaluzCommented:
Just in case you'll entertain Linq rather than Regex, here's my suggestion.
I assume that the lines to search are read from file into an array, using linq I would:
Dim rStr = {"system-serial-number:70000081",
            "partner-system-id:0151698606",
            "partner(-System - Name) : frehfas82b",
            "Controller(-address) : AddHandler",
            "number-of-processors:4",
            "memory-size:16376"}
Dim Query = From p In rStr Where p.Contains("number-of-processors") Select p.Split(":").Last
Dim SName = From p In rStr Where p.Contains("partner-system-name") Select p.Split(":").Last

Open in new window

In the case of the system-name.... query where it would not necessarily always exist, do a count test before attempting to access the results, e.g
If SName.Count > 0 Then ....

Open in new window

0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
vbighamCommented:
Try this:
Private Sub Test()
    Dim strRegex As String = "^(?:number-of-processors:)(?<ProcessorCount>\d+)"
    Dim myRegexOptions As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Multiline
    Dim myRegex As New Regex(strRegex, myRegexOptions)
    Dim strTargetString As String = _
        "system-serial-number:70000081 " & ChrW(10) & _
        "partner-system-id:0151698606 " & ChrW(10) & _
        "partner-system-name:frehfas82b " & ChrW(10) & _
        "Controller-address:A " & ChrW(10) & _
        "number-of-processors:4 " & ChrW(10) & _
        "memory-size:16376 " & ChrW(10) & _
        "system-serial-number:70000081 " & ChrW(10) & _
        "partner-system-id:0151698606 " & ChrW(10) & _
        "partner-system-name:frehfas82b " & ChrW(10) & _
        "Controller-address:A " & ChrW(10) & _
        "number-of-processors:8 " & ChrW(10) & _
        "memory-size:16376" & ChrW(10)

    Dim procCount As Integer
    Dim allProcCounts As New List(Of Integer)

    For Each myMatch As Match In myRegex.Matches(strTargetString)
        If myMatch.Success Then
            If Integer.TryParse(myMatch.Groups("ProcessorCount").Value, procCount) Then
                allProcCounts.Add(procCount)
            End If
        End If
    Next

    For Each i In allProcCounts
        Console.WriteLine(i)
    Next
End Sub

Open in new window

0
 
newimagentAuthor Commented:
I'm not sure i can fit either of those into my current code.

Dim cpuCount As String= (Regex.Match(slotConfigData,

slotConfigData is the string array I am pulling the data from, and I want to locate a specific value and assign it to cpuCount. Is there no way to do this as I've started to show above?
0
 
newimagentAuthor Commented:
Below is the full code i'm using at this point to populate a gridview. It would be great if i can build the regex expression just off the specific variables that need to be located via an expression.

for cpu count the line in the array looks like this:
number-of-processors:4  (where 4 is the value that changes based on the log files the data is read from)


'Create "FilerData" Data Table
        Dim Table1 As DataTable
        Table1 = New DataTable("FilerData")
        'Declare column called Filer Name
        Dim filerName As DataColumn = New DataColumn("Filer Name")
        'Set Data Type for the column
        filerName.DataType = System.Type.GetType("System.String")
        'Add column to table
        Table1.Columns.Add(filerName)

        Dim filerModel As DataColumn = New DataColumn("Model")
        filerModel.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(filerModel)

        Dim Ontap As DataColumn = New DataColumn("Ontap")
        Ontap.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(Ontap)

        Dim sysID As DataColumn = New DataColumn("SysID")
        sysID.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(sysID)

        Dim sysSerial As DataColumn = New DataColumn("Serial")
        sysSerial.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(sysSerial)

        Dim sysMem As DataColumn = New DataColumn("Memory")
        sysMem.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(sysMem)

        Dim sysCPU As DataColumn = New DataColumn("CPU")
        sysCPU.DataType = System.Type.GetType("System.String")
        Table1.Columns.Add(sysCPU)


        For Each slotConfigInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_SlotConfigInfo.log", SearchOption.AllDirectories)

            Dim slotConfigData As String() = IO.File.ReadAllLines(slotConfigInfo)
            Dim systemName As String = slotConfigData(4).Substring(14)
            Dim systemID As String = slotConfigData(6).Substring(12)
            Dim systemModel As String = slotConfigData(8).Substring(15)
            Dim systemSerialNumber As String = slotConfigData(14).Substring(23)
            'Dim cpuCount As String = slotConfigData(30).Substring(23)
            Dim cpuCount As String= (Regex.Match(slotConfigData, 
            Dim memorySize As String = slotConfigData(32).Substring(14)
            'Declare 1 Row for data
            Dim Row1 As DataRow


            Try
                'Declare new row
                Row1 = Table1.NewRow()
                'Fill row with data - Item property is used to set the field value
                Row1.Item(filerName) = systemName
                Row1.Item(filerModel) = systemModel
                'Row1.Item(Ontap) = ontapVersion
                Row1.Item(sysID) = systemID
                Row1.Item(sysSerial) = systemSerialNumber
                Row1.Item(sysMem) = memorySize
                Row1.Item(sysCPU) = cpuCount
                Table1.Rows.Add(Row1)
            Catch ex As Exception
            End Try

        Next

        For Each versionInfo As String In IO.Directory.GetFiles(txtPath.Text & "\StorageSystem\", "*_VersionInfo.log", SearchOption.AllDirectories)
            Dim versionInfoData As String() = IO.File.ReadAllLines(versionInfo)
            Dim ontapVersion As String = versionInfoData(2).Substring(24, 9)
            Dim Row1 As DataRow

            Try
                Row1 = Table1.NewRow()
                Row1.Item(Ontap) = ontapVersion
                Table1.Rows.Add(Row1)
            Catch ex As Exception
            End Try

        Next
        'Bind grid to DataTable
        gridFilerProperties.DataSource = Table1
        gridFilerProperties.DataBind()

Open in new window

0
 
nepaluzCommented:
The code will go in the same place you have defined the variables (after you read the file to the slotConfigData String()) as per below:
Dim cpuCount = From p In slotConfigData Where p.Contains("number-of-processors") Select p.Split(":").Last
Dim systemName = From p In slotConfigData Where p.Contains("partner-system-name") Select p.Split(":").Last

Open in new window

Could you provide a sample of both files from which you are retrieving the data from? I believe you can achieve your objective with a lot less code using linq than you have at the moment (not to mention theother code you need to add to it!).
My code suggestion above will
Point to note though, using LINQ will "bloat" your executable as compared to working with arrays and strings et al as you are doing, but the bloating is relative. On the other hand, regex will have an overhead with the processing, but again, the overhead is relative.
0
 
newimagentAuthor Commented:
Sure the files read into the arrays are below.

When i try to use your code above my table output for cpu count is:

System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String]
Version Info File:

results:

	version:Release 7.3.1.1P3: Mon Jun  8 11:14:54 PDT 2009


Slot Config Info File:

results:

	system-info:

		system-name:frehfas81a

		system-id:0151698219

		system-model:FAS3170

		system-machine-type:FAS3170

		system-serial-number:70000081

		partner-system-id:0151698606

		partner-system-name:frehfas82b

		system-revision:A0

		controller-address:A

		board-speed:2599

		cpu-part-number:110-00119

		cpu-revision:A3

		cpu-serial-number:703275

		cpu-firmware-release:4.1.0

		number-of-processors:4

		memory-size:16376

		cpu-processor-id:0x40f13

		supports-raid-array:false

results:

	fcp-config-adapters:

		fcp-config-adapter-info:

			adapter:0c

			node-name:50:0a:09:80:89:9a:bc:ae

			port-name:50:0a:09:83:99:9a:bc:ae

			port-address:746752

			adapter-type:Local

			media-type:auto

			state:online

			standby:false

			speed:auto

		fcp-config-adapter-info:

			adapter:0d

			node-name:50:0a:09:80:89:9a:bc:ae

			port-name:50:0a:09:81:99:9a:bc:ae

			port-address:812288

			adapter-type:Local

			media-type:auto

			state:online

			standby:false

			speed:auto

		fcp-config-adapter-info:

			adapter:0a

			node-name:50:0a:09:80:89:9a:bc:ae

			port-name:50:0a:09:84:99:9a:bc:ae

			port-address:726272

			adapter-type:Local

			media-type:auto

			state:online

			standby:false

			speed:auto

		fcp-config-adapter-info:

			adapter:0b

			node-name:50:0a:09:80:89:9a:bc:ae

			port-name:50:0a:09:82:99:9a:bc:ae

			port-address:791808

			adapter-type:Local

			media-type:auto

			state:online

			standby:false

			speed:auto

Open in new window

0
 
nepaluzCommented:
when adding it to the column, do:
Row1.Item(sysCPU) = cpuCount(0)

Open in new window

or
Row1.Item(sysCPU) = cpuCount.First

Open in new window

if you need to test it first, do:
Row1.Item(sysCPU) = If(cpuCount.Count > 0, cpuCount.First, "Error Value")

Open in new window

I'll look at the file and see what I can come up with
0
 
newimagentAuthor Commented:
That is excellent. Thank you!

Can you answer one other question? The line in version info. Ideally I would like to get the data after "Release " and before the ":"  ... so just the release number. I coded the max number of characters to return which works except on short version numbers it adds the ":" to my output.
0
 
nepaluzCommented:
1. The line is: version:Release 7.3.1.1P3: Mon Jun  8 11:14:54 PDT 2009
Dim ontapVersion = From q in versionInfoData Where q.StartsWith("version") Select q.Split(":")(1).Replace("Release", String.Empty).Trim

Open in new window

have not tested the query but should work (fingers crossed). Again, do not forget to add the test and choosing the first item in the list.
0
 
newimagentAuthor Commented:
That code returns an empty string, or the empty space from the string
0
 
newimagentAuthor Commented:
Just to clarify this is the line:

version:NetApp Release 7.3.1.1P3: Mon Jun  8 11:14:54 PDT 2009

I want the character after "Release " to the ":" after 7.3.1.1P3  ... there are multiple ":" in this line ... not sure if thats screwing this up
0
 
newimagentAuthor Commented:
Figured out my last question with some mods to your code:

Dim ontapVersion = From q In versionInfoData Where q.Contains("NetApp Release ") Select q.Split(":")(1).Replace("NetApp Release", String.Empty).Trim

Thank you so much for the great help on this!
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now