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

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:

<< 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")

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 :wink:

1 Like