add tag
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.

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.