How to do readuntil(io, ::Regex)?

ettersi
Wed, 07 Feb 2024 07:31:18 +0000
General Usage

I have a TCP connection over which I can send a command and then the remote end responds with some output followed by either OK or ERROR. For example:

>> GETSTATE
<< 0002   # Some status code
<< OK

To make it easier to work with such a connection, I would like to have a function execute() which behaves roughly as follows.

function execute(connection::IO, command::String)
    println(connection, command)
    return readuntil(connection, r"OK|ERROR")
end

Unfortunately, readuntil(io, ::Regex) does not seem to work.

julia> readuntil(IOBuffer("test"), r"")
ERROR: The IO stream does not support reading objects of type Regex.

Is such a functionality implemented somewhere in the Julia ecosystem?

For reference, Java has Scanner.findWithinHorizon() which does exactly what I want. If Julia doesn’t have something like this yet, then I can probably hack something together using BufferedStreams.jl, but doing this right is tricky enough that I’d rather freeride on the work of others

1 post - 1 participant