Regex 在Vb.net中用正则表达式返回invaild数据

Regex 在Vb.net中用正则表达式返回invaild数据,regex,vb.net,Regex,Vb.net,下面是我的代码 Dim ReadTagdata As String = "Info,Reply:Axis 0, Channel G, Timestamp 2eff, SpecificID f00ba36d, CRC d01ed74e, ReadAddress 000a, Data: 7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 642c 462c 5653 3

下面是我的代码

  Dim ReadTagdata As String = "Info,Reply:Axis 0, Channel G, Timestamp 2eff, SpecificID f00ba36d, CRC d01ed74e, ReadAddress 000a, Data: 7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 642c 462c 5653 322c 7665 7279 2067 6f6f 642c 676f 6f64 2c76 6572 7920 676f 6f64 2c6d 6564 6975 6d2c 4852 442c 3137 3530 3234 3837 3333 2c32 352d 4175 672d 3130 2c33 2e39 342c 332e 3932 2c32 2e38 322c 332e 3839 2c35 362c 3131 2e35 2c34 332c 6d65 6469 756d 2c70 6f69 6e74 6564 2c2c 2c2c 2c2c 4e2c 2c2c 2c2c 2c2c 2c2c"

    Dim Specificcol As MatchCollection = Regex.Matches(ReadTagdata, "SpecificID ([a-f0-9]{8})")
    For Each m As Match In Specificcol
        Dim g As Group = m.Groups(1)
        MsgBox(g.Value.ToString)
    Next


    Dim DataCol As MatchCollection = Regex.Matches(ReadTagdata, "Data: ([A-Za-z0-9\-]+) ", RegexOptions.IgnoreCase)
    For Each mdata As Match In DataCol
        Dim gdata As Group = mdata.Groups(1)
        Dim data As String = gdata.Value.ToString
        MsgBox(data)
    Next
在我试图检索的上述代码中,需要使用正则表达式从ReadTagData字符串中获取数据 表达 但使用以下代码,我只能在数据之后检索1个值:字符串,即7a7a, 相反,我希望在数据之后添加所有字符串:“字符串”

Dim DataCol As MatchCollection=Regex.Matches(ReadTagdata,“数据:([A-Za-z0-9-]+)”,RegexOptions.IgnoreCase)


提前感谢

此正则表达式:

Data:\s([\w\s\-])+
Data:\s([\w\s\-]+)
与此匹配:

Data: 7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 6-42c 462c 5653 322c 7665 7279 2067 6f6f 642c 676f 6f64 2c76 6572 7920 676f 6f64 2c6d 6564 6975 6d2c 4852 442c 3137 3530 3234 3837 3333 2c32 352d 4175 672d 3130 2c33 2e39 342c 332e 3932 2c32 2e38 322c 332e 3839 2c35 362c 3131 2e35 2c34 332c 6d65 6469 756d 2c70 6f69 6e74 6564 2c2c 2c2c 2c2c 4e2c 2c2c 2c2c 2c2c 2c2c
7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 6-42c 462c 5653 322c 7665 7279 2067 6f6f 642c 676f 6f64 2c76 6572 7920 676f 6f64 2c6d 6564 6975 6d2c 4852 442c 3137 3530 3234 3837 3333 2c32 352d 4175 672d 3130 2c33 2e39 342c 332e 3932 2c32 2e38 322c 332e 3839 2c35 362c 3131 2e35 2c34 332c 6d65 6469 756d 2c70 6f69 6e74 6564 2c2c 2c2c 2c2c 4e2c 2c2c 2c2c 2c2c 2c2c
以及此正则表达式的
1:

Data:\s([\w\s\-])+
Data:\s([\w\s\-]+)
与此匹配:

Data: 7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 6-42c 462c 5653 322c 7665 7279 2067 6f6f 642c 676f 6f64 2c76 6572 7920 676f 6f64 2c6d 6564 6975 6d2c 4852 442c 3137 3530 3234 3837 3333 2c32 352d 4175 672d 3130 2c33 2e39 342c 332e 3932 2c32 2e38 322c 332e 3839 2c35 362c 3131 2e35 2c34 332c 6d65 6469 756d 2c70 6f69 6e74 6564 2c2c 2c2c 2c2c 4e2c 2c2c 2c2c 2c2c 2c2c
7a7a 3433 3132 3334 3536 3738 3637 2c31 352c 302e 3036 2c2e 3930 2d31 2e32 322c 526f 756e 6-42c 462c 5653 322c 7665 7279 2067 6f6f 642c 676f 6f64 2c76 6572 7920 676f 6f64 2c6d 6564 6975 6d2c 4852 442c 3137 3530 3234 3837 3333 2c32 352d 4175 672d 3130 2c33 2e39 342c 332e 3932 2c32 2e38 322c 332e 3839 2c35 362c 3131 2e35 2c34 332c 6d65 6469 756d 2c70 6f69 6e74 6564 2c2c 2c2c 2c2c 4e2c 2c2c 2c2c 2c2c 2c2c

注:

\w=字母数字字符加上“u”

\s=空格字符

\-=“-”字符


为了在将来测试更多的正则表达式,我将向您推荐一个简单的正则表达式测试程序,它是我很久以前开发的,这就是我如何处理您需要的这些表达式的方法:


在这里下载源代码:

Thanx以获取帮助,但是Dim DataCol作为MatchCollection=Regex.Matches(ReadTagdata,“Data:\s([\w\s\-])+),我只会得到一个字符“c”,您需要使用这个Regex
数据:\s([\w\s\-])
和匹配组
1
Dim DataCol As MatchCollection=Regex.Matches(ReadTagdata,“Data:\s([\w\s\-]+)”,RegexOptions.IgnoreCase)
Dim Matches As MatchCollection=Regex.Matches(ReadTagdata,pattern,RegexOptions.IgnoreCase)作为Matches MsgBox(match.Groups(1).Value)下一步的匹配