Home > Enterprise >  Regex return a previous match as a match
Regex return a previous match as a match

Time:08-11

I have a regex that matches [String] and ID. Sometimes, the second, third, etc. instance of [String] has ID lines that are blank, but the first instance of [String] always has an ID line. Right now, the 2nd, 3rd, etc. instance are returning null ID as they should because the line is blank. I am trying to retrieve the ID match from the first instance of [String] and assign it to the other instances of the same [String]. Example:

[String]
ID=18EEEEEE //
Blah
Blah
Blah
Var=Blah unsigned 8,8 /name:"Blah" /number:2542
Var=Blah unsigned 40,24 /name:"Blah" /number:2543
Output: String, EEEE, 8,8, Blah, 2542, 40,24, Blah, 2543

[String] 'Shares same ID as above because it is same name [String], but currently ID is blank. Need the above ID (EEEE)
Blah
Blah
Var=Blah unsigned 8,8 /name:"Blah" /number:2544
Var=Blah unsigned 40,24 /name:"Blah" /number:2545
Output: String, 8,8, Blah, 2544, 40,24, Blah, 2545. 
Ideal Output: String, EEEE, 8,8, Blah, 2544, 40,24, Blah, 2545
Function ReadFile() As List(Of Mapping)
        Dim mappings As New List(Of Mapping)()
        Dim P, line, startPosition, totalLength, id, name, number As String
        Dim m As Match
        Dim lastId As String
        Do
            Dim mapping As New Mapping()
            line = reader.ReadLine
            If line Is Nothing Then Continue Do
            m = Regex.Match(line, "^\[(\w )\]")
            P = m.Groups(1).ToString()
            mapping.P = P
            ' Grab that code
            If m.Groups.Count > 1 Then
                ' Read the next line after the code and get the 4 digits
                'If next line is blank with no ID, use the previous ID from the 4 digit match
                line = reader.ReadLine
                m = Regex.Match(line, "ID=\w\w(\w\w\w\w)")
                id = m.Groups(1).ToString()
                If id = "" Then
                    id = lastId
                Else
                    mapping.id = id
                    lastId = id
                End If

CodePudding user response:

If the additional instances of [String] are always after the first one like in your example, you don't need to do much else. Do not set a new value to the ID and it will retain the previous one while it is looping through the rest of the lines. As long as you do not set it to null it will not be null.

So in the code above, simply do not do anything if the regex does not match.

CodePudding user response:

If I understand your question correctly, all you need to do is store the last-encountered ID in a variable so you can keep using that value, as needed, until it changes. So, for instance, see where I added the lastId variable into your code:

Function ReadFile() As List(Of Mapping)
    Dim mappings As New List(Of Mapping)()
    Dim P, line, startPosition, totalLength, id, name, number As String
    Dim m As Match
    Dim lastId As String  ' *** Here ***
    Do
        Dim mapping As New Mapping()
        line = reader.ReadLine
        If line Is Nothing Then Continue Do
        m = Regex.Match(line, "^\[(\w )\]")
        P = m.Groups(1).ToString()
        mapping.P = P
        ' Grab that code
        If m.Groups.Count > 1 Then
            line = reader.ReadLine
            m = Regex.Match(line, "ID=\w\w(\w\w\w\w)")
            If m Is "" Then
                id = lastId  ' *** Here ***
            Else
                id = m.Groups(1).ToString()
                mapping.id = id
                lastId = id  ' *** Here ***
            End If
  • Related