Regexp icon indicating copy to clipboard operation
Regexp copied to clipboard

Capture returns binary value?

Open JamesNewton opened this issue 7 years ago • 3 comments

What would it take to return, for example, the /binary/ value matched by the %x or %d codes. E.g. if you had a pattern like (%x+),(%x+) and you fed it "0F,09" you could get back matches of "0F" and another of "09" just like now, but also get back an array of binary values, containing 15 and 9. In other words, as long as you are matching numbers, convert them to binary values at the same time.

This comes from my thinking and question here: https://arduino.stackexchange.com/questions/50170/extract-binary-data-from-text-string-like-perl-pack-not-just-regexp

Probably not worth doing? But I figured I would ask just incase other people see value in that idea.

JamesNewton avatar Feb 24 '18 01:02 JamesNewton

The returned captures are always strings, because it just remembers the starting and ending position inside your supplied target. Turning them into something else would require memory allocation, and confusion about whether a certain pattern really is hex, decimal, or something else. See my answer on the linked SE question.

nickgammon avatar Feb 24 '18 21:02 nickgammon

Good point about memory allocation. How about returning the text of the regexp that matched the captured text (or rather pointers to it) in addion? The goal being to allow a user to configure the type of the extracted data without a separate config item. If the capturing expression is %x+ my code can convert the captured text from hex. If its %d+, it can convert from decimal.

JamesNewton avatar Feb 24 '18 21:02 JamesNewton

The regexp library is basically the Lua pattern matching library with an interface for use outside Lua. I don't want to start fiddling with it. If you want to know what each capture was, you could run a regexp on the regexp (eg. looking for whatever was inside round brackets) but I think you are overthinking this.

nickgammon avatar Feb 25 '18 05:02 nickgammon