Home > Blockchain >  Vb.net regex finding a pattern after 5 spaces and 2 characters
Vb.net regex finding a pattern after 5 spaces and 2 characters

Time:07-30

I am trying to work on this regex pattern and match below examples. There are 5 spaces after Rx which I have tried to use " *", for but no luck.

("RX," *",\w\w(\w\w\w\w))

1)     18468.0  Rx     1CEBF900  8  02 00 00 80 00 01 01 FF - ' should match EBF9 
2)     18468.6  Rx     18FD4000  8  FF FF 00 FF FF FF FF FF - 'should match FD40
ETC . . .

CodePudding user response:

This expression seems to work:

Rx\s{5}\S{2}(.{4})
Function GetValue(line As String) As String
    Dim regex As New Regex("Rx {5}\S{2}(.{4})")
    Dim match As Match = regex.Match(line)
    If match.Success Then Return match.Groups(1).Value
    Return Nothing        
End Function

See it here:

https://dotnetfiddle.net/yY3xXX

CodePudding user response:

With your shown samples and attempts please try following regex and vb.net code. This will result EBF9 and FD40 values in output. Here is the Online demo for used regex in following.

Regex used for solution is:(?<=\s Rx\s{5}.{2})\S (?=\d{2}).

Imports System.Text.RegularExpressions
    
Module Module1
    Sub Main()

        Dim regex As Regex = New Regex("(?<=\s Rx\s{5}.{2})\S (?=\d{2})")
        Dim match As Match = regex.Match("18468.0  Rx     1CEBF900  8  02 00 00 80 00 01 01 FF")
        If match.Success Then
        Console.WriteLine("RESULT: [{0}]", match)
        End If        

        Dim match1 As Match = regex.Match("18468.6  Rx     18FD4000  8  FF FF 00 FF FF FF FF FF")
        If match1.Success Then
            Console.WriteLine("RESULT: [{0}]", match1.Value)
        End If
        
    End Sub
End Module

Explanation of regex:

(?<=\s Rx\s{5}.{2}) ##Positive look behind to make sure Rx followed 
                    ##by 5 spaces followed by 2 any characters present.
\S                  ##matching all non-spaces here.
(?=\d{2})           ##Making sure they are followed by 2 digits.

Also I have taken both of your sample lines in 2 different variables just to show 2 lines output.

CodePudding user response:

Here is a pattern that seems to extract the specific data you're seeking. It was generated and tested via RegExr.

Search Pattern: /(Rx {5}[0-9A-F]{2})([0-9A-F]{4})/g; List/Replace Pattern: $2

Description: the first capture group specifies "Rx", five spaces, and two hexadecimal range characters; the second capture group specifies the next four hexadecimal range characters.

  • Related