SpikETidE
SpikETidE

Reputation: 6961

Applying Regex over a Stream to extract a string

We are using a REST API call that returns a string of a very large size. We are performing

httpget.getResponseBodyAsString();

to get the string returned by the REST call. Then we apply regex over this string to extract the substring that we require. When the string returned by the REST call is huge, we are facing Out Of Memory issues with the JVM.

We can also get the data from the REST call as a stream using

httpget.getResponseBodyAsString();

But is it possible to apply regex over the stream and extract the string that we require?

Upvotes: 0

Views: 409

Answers (1)

Drew MacInnis
Drew MacInnis

Reputation: 8587

These previous answers show a few options:

  1. Performing regex on a stream
  2. Applying a regular expression to a Java I/O Stream

I think that Scanner.findWithinHorizon mentioned by the first answer (above) may be an interesting option.

Upvotes: 2

Related Questions