Closed romanlehnert closed 7 years ago
Have you read Capturing input section in this article? Something like this should work:
require 'parslet'
include Parslet
ele =
str('@') >>
match('\\d').repeat.capture(:size) >>
str('@') >>
dynamic { |s, c| any.repeat(c.captures[:size].to_i, c.captures[:size].to_i) }
ele.parse('@4@abcd')
Hey there,
i'm trying to figure out how to parse string that may contain binary data, which is prefixed with the byte-length inside the string and may contain unescaped control characters.
Regularly, the elements in the string are separated by a
:
. And when the:
appears inside a string, it is escaped by a?
.Example:
Should parse to
But such an element may also contain binary data. This binary data
:
)@length@data
Within a row, it may look like this:
Should parse to
What is the best way to handle this with parslet? I'm really thankful for any advise.