0

I would like to be able to obtain a list of regular expressions from a file. I have tried reading the regexes into a char * or a std::string which works without a problem. However, converting them/using them as regexes proves to be fruitless, since there is a mismatch of data types.

Most online resources I have found create regexes such as this:

std::tr1::regex rx("regex expression here");

Pardon, I am also not sure of the difference between:

std::tr1::regex rx("regex expression here");

opposed to

std::tr1::regex rx = ("regex expression goes here");

I would like to know if there is a way to convert any kind of string data type into a regex in C++. Any help is appreciated. Thank you!

user515751
  • 19
  • 3
  • 1
    Define "fruitless". Show your error. If you can construct a `regex` from a string literal, you can construct a `regex` from a `char const*` (yes `const`, get into this habit please) or `std::string`. Also, I fixed your example. – Lightness Races in Orbit Jun 15 '11 at 16:43
  • 1
    Can you supply code frag of what you've tried? Note sure why things like "assign" won't work for you with that description: http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a00878.html#360cff93aab9de0d81f206d724b1c695 – holtavolt Jun 15 '11 at 16:43
  • @ereOn @DumbCoder: Nice one. Now he's just gone and accepted an arbitrary answer to each of his questions. That _really_ helps the community. – Lightness Races in Orbit Jun 15 '11 at 16:48
  • 1
    `std::tr1::regex` has a ctor that takes an `std::string` to specify the regex. Reading data into an `std::string` is common and trivial. – Jerry Coffin Jun 15 '11 at 16:49

1 Answers1

0

Simply load file to some char* (I assume that you can do this) and pass it to regex.

Xirdus
  • 2,997
  • 6
  • 28
  • 36
  • I figured out the problem. I was using the C function fgets() to read the regular expression from the file, which would also copy the null byte. Thanks Xirdus, I pinpoint the problem was after your suggestion. – user515751 Jun 17 '11 at 18:06