0

For example, mystring is = "53 54 41 52 54 00 00 01 53 54 41 52 54 00 01 02 53 54 41 52 54 01 02 03"

What I want to do is every time I found "53 54 41 52 54" it will capture the remaining 3 bytes. So, based on the example of my string above, I want the result as below

When it first found 53 54 41 52 54, it will capture 00 00 01 then, when it found the 2nd 53 54 41 52 54, it will capture 00 01 02 then, finally when it found the 3rd 53 54 41 52 54, it will capture 01 02 03.

What is the best way to do this in VB?

Thank you in advance.

Appreciate your guidance.

1
  • You refer to the character pairs as bytes - is there a possibility of getting something like A2 or 162? Commented Feb 12, 2020 at 9:34

2 Answers 2

2

You ca use a regular expression:

Dim r as New Regex("53 54 41 52 54 (?<b>\d\d \d\d \d\d)")

ForEach m as Match in r.Matches(mystring)
  MsgBox(m.Groups("b").Value)
Next m

Regular expression will seek your desired digits then take the subsequent 3 pairs of \digits and capture them into a group named b which you can retrieve as shown in the loop

Sign up to request clarification or add additional context in comments.

Comments

0

An alternative (less neat, but possibly easier to read if unfamiliar with Regular Expressions) option would be:

    Const myString As String = "53 54 41 52 54 00 00 01 53 54 41 52 54 00 01 02 53 54 41 52 54 01 02 03"
    Const myMatch As String = "53 54 41 52 54"
    Dim Results As New List(Of String)
    Dim i As Integer = 0
    Do While i >= 0 AndAlso i < myString.Length
        i = myString.IndexOf(myMatch, i)
        If i >= 0 Then
            Results.Add(myString.Substring(i + 15, 8))
            i += 9
        End If
    Loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.