Vb.net正则表达式在5个空格和2个字符后找到一个模式



我正在尝试处理这个正则表达式模式,并匹配下面的示例。Rx后面有5个空格,我曾尝试使用" *",,但没有成功。

("RX," *",ww(wwww))

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

这个表达式似乎有效:

Rxs{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

点击此处查看:

https://dotnetfiddle.net/yY3xXX

这里有一个模式,它似乎可以提取您要查找的特定数据。它是通过RegExr生成和测试的。

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

描述:第一捕获组指定";Rx";,五个空格和两个十六进制范围字符;第二个捕获组指定接下来的四个十六进制范围字符。

使用您显示的示例和尝试,请尝试以下regex和vb.net代码。这将导致输出中的EBF9FD40值。下面是所用正则表达式的在线演示。

用于溶液的Regex为:(?<=s+Rxs{5}.{2})S+(?=d{2})

Imports System.Text.RegularExpressions

Module Module1
Sub Main()
Dim regex As Regex = New Regex("(?<=s+Rxs{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+Rxs{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.

另外,我在两个不同的变量中都取了两条采样线,只是为了显示两条线的输出。

最新更新