or
Jonathan Mee
I want to call [`regex_match`](https://en.cppreference.com/w/cpp/regex/regex_match) or similar on every line in a file conditionally operating on whether there was a match. Is there a way to do this without slurping the entire file?
Top Answer
Jonathan Mee
No, but you can make a function which uses [`getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) on the stream and applies the result pretty easily:

    void Foo(const std::regex& r, std::istream& stream) {
      std::string i;
      std::smatch m;

      while(std::getline(stream, i)) {
        if(std::regex_match(i, m, r)) {
            // Operate on matching line here
        } else {
            // Operate on non-matching line here
        }
      }
    }

I give an example using an `istringstream` [here](https://ideone.com/A65yAQ) but this could be substituted with any stream.
Can I use a Regex on a Stream?

This is a dedicated room for discussion about this question.

Once logged in you can direct comments to the question poster (or any answer poster) here.