0

I have the following code:

#include <boost/regex.hpp>

#include <iostream>
#include <string>

int main()
{
  const std::string input_data("2014-02-20 12:32:15");
  const boost::regex reg_exp("[-:\\d\\s]+");
  boost::smatch results;
  std::cout << boost::regex_match(input_data, results, reg_exp) << '\n';
}

Output

1

If I change a regular expressions from

"[-:\\d\\s]+"

to

"[\\d:-\\s]+"

this code will output "0", while http://www.regexr.com/ tells that everything is fine. Does boost think that ":-\s" is a range of characters? Is it defined in the regular expression's standard? Who's right?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • My understanding is that if you want a literal `-` in a bracket expression it should be the first character. If it's not the first character, it should be escaped with a \. – Sean Bright Feb 06 '14 at 13:03
  • 1
    Actually, [Wikipedia says](http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended) that the `-` can be the first or last character and be treated literally. – Sean Bright Feb 06 '14 at 13:10

1 Answers1

0

while http://www.regexr.com/ tells that everything is fine. Does boost think that ":-\s" is a range of characters?

Boost and regexr simply does not use the same regex flavor. This is why you see different output with the same regular expression. Regexr uses the flavor shipped with ActionScript. On the other hand, boost supports Perl, POSIX Basic and POSIX extended regexes syntax.

Is it defined in the regular expression's standard? Who's right?

Again, it all depends on the flavor your regex runs on. Some flavor may support one feature while other not.

If you want to test your regexes before using them in your code, I suggest you using bregextest.

Stephan
  • 41,764
  • 65
  • 238
  • 329