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

**URL:** <https://discourse.julialang.org/t/how-to-do-readuntil-io-regex/109855>\
**Category:** General Usage\
**Tags:** regex, io\
**Created:** [February 7, 2024, 7:31am UTC](https://discourse.julialang.org/t/how-to-do-readuntil-io-regex/109855 "2024-02-07T07:31:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [February 7, 2024, 7:31am UTC](https://discourse.julialang.org/t/how-to-do-readuntil-io-regex/109855/1 "2024-02-07T07:31:18Z")

</div>

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:

```julia
>> 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.

```julia
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
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()`](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#findWithinHorizon-java.lang.String-int-) 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 😉
