add tag
Jonathan Mee
I want to call [`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`]( 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]( 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.