繁体   English   中英

Z6145AFC977C7​​660083BDCDDEA91B7D09Z 正则表达式在 5 个空格和 2 个字符后找到一个模式

[英]Vb.net regex finding a pattern after 5 spaces and 2 characters

我正在尝试使用此正则表达式模式并匹配以下示例。 Rx 后面有 5 个空格,我尝试使用" *",但没有运气。

("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 . . .

使用您显示的示例和尝试,请尝试遵循正则表达式和 vb.net 代码。 这将导致EBF9中的 EBF9 和FD40值。 下面是使用的正则表达式的在线演示

用于解决方案的正则表达式是: (?<=\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

正则表达式的解释:

(?<=\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.

此外,我在 2 个不同的变量中取了你的两个示例行,只是为了显示 2 行 output。

这个表达式似乎有效:

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

在这里看到它:

https://dotnetfiddle.net/yY3xXX

这是一种似乎提取您正在寻找的特定数据的模式。 它是通过RegExr生成和测试的。

搜索模式: /(Rx {5}[0-9A-F]{2})([0-9A-F]{4})/g ; 列出/替换模式: $2

说明:第一个捕获组指定“Rx”,五个空格,两个十六进制范围字符; 第二个捕获组指定接下来的四个十六进制范围字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM