简体   繁体   中英

regex issue with non printable chars

I'm trying to create a regex that returns one or more matches from a string and it's driving me nuts.

(?<=\x03).*?(?=\r)

The above should be looking for a start char of ETX (Char 3) and an end char of Carriage return.

I want the text between each start and end char "block".

What am I doing wrong?

Actually ignore me it does work. Think I was getting confused during debugging an the matches attribute looking to be zero. A small test proves it is ok (in VB).

    Dim TestString1 As String = Chr(3) & "Some Text to Extract 1" & Chr(13)
    Dim TestString2 As String = Chr(3) & "Some Text to Extract 2" & Chr(13)
    Dim TestString3 As String = Chr(3) & "Some Text to Extract 3" & Chr(13)

    Dim TestString As String = TestString1 & " random noise " & TestString2 & " more random noise " & TestString3
    Dim Matches As MatchCollection = Regex.Matches(TestString, "(?<=\x03).*?(?=\r)")

    Dim Index As Integer = 0
    Dim result As New StringBuilder
    result.Append("No of Matches found = " & Matches.Count & vbCrLf & vbCrLf)
    For Each m As Match In Matches
        Index += 1
        result.Append(Index & ": " & m.Groups(0).Value & vbCrLf)
    Next

    MessageBox.Show(result.ToString())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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