2

Actually, I try to find a regular expression in a multilines string, but I think I'm on the wrong way to find the next regular expression after a new line (equals to a '\n'). Here is my regex :

#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>

#define USE ".*[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}.*(?:\\n)*"

int                     main(int argc, char **argv)
{
  std::stringstream     stream;
  std::filebuf          *buffer;
  std::fstream          fs;
  std::string           str;
  std::regex            regex(USE);

  if (argc != 2)
    {
      std::cerr << "Think to the use !" << std::endl;
      return (-1);
    }
  fs.open(argv[1]);
  if (fs)
    {
      stream << (buffer = fs.rdbuf());
      str = stream.str();
      if (std::regex_match(str, regex))
        std::cout << "Yes" << std::endl;
      else
        std::cout << "No" << std::endl;
      fs.close();
    }
  return (0);
}
Meugiwara
  • 599
  • 1
  • 7
  • 13
  • Sorry, the code does not clarify welll what you are trying to achieve. Please explain the requirements, provide the sample text and expected output. – Wiktor Stribiżew Apr 12 '17 at 09:27
  • @WiktorStribiżew, I try to open a text file, read inside and stock characters in a string and use a regex on the string to find a number like in my regex but I can't parse multilines. That's my issue. – Meugiwara Apr 12 '17 at 09:31
  • Could you please provide an [MCVE (minimal complete verifiable example)](http://stackoverflow.com/help/mcve)? – Wiktor Stribiżew Apr 12 '17 at 09:33
  • @WiktorStribiżew, I'll try to post an example simplyfied. – Meugiwara Apr 12 '17 at 09:38

1 Answers1

2

There are some flags that can be specified when constructing regex object, see documentation http://en.cppreference.com/w/cpp/regex/basic_regex for details.

Short working example with regex::extended flag, where newline character '\n' is specified in search follows:

#include <iostream>
#include <regex>

int main(int argc, char **argv)
{
  std::string str = "Hello, world! \n This is new line 2 \n and last one 3.";
  std::string regex = ".*2.*\n.*3.*";
  std::regex reg(regex, std::regex::extended);

  std::cout << "Input: " << str << std::endl;

  if(std::regex_match(str, reg))
    std::cout << "Match" << std::endl;
  else
    std::cout << "NOT match" << std::endl;

  return 0;
}
pe3k
  • 796
  • 5
  • 15