user1357687
user1357687

Reputation: 631

c++11 std::regex bug?

I tried to use the regex library in c++11 on OSX using clang:

// product format
//   "AAPL  150918C00099500"
// python regex
//   "(?P<Symbol>[a-zA-Z0-9]+)\s*(?P<Expiry>\d{6})(?P<Payoff>[C|P])(?P<Strike>\d{8})"
#include <string>
#include <regex>
#include <iostream>

int main()
{
   std::string s{ "AAPL  150918C00099500" };
   std::regex pat{ R"([a-zA-Z0-9]{1,6})\s*(\d{6})([CP]{1})(\d{8})" };
   bool isMatch = std::regex_match( s, pat );
   std::sregex_iterator it( s.begin(), s.end(), pat );
   for( ; it != std::sregex_iterator{}; ++it )
   {
      std::cout << ( *it )[0] << std::endl;
   }
}

The output of the code below should be:

AAPL
150918
C
00099500

Instead it spits out

AAPL
150918
C00099
500

This seems like a bug... Does anybody know of a way around this ?

Thanks

System details:

$  uname -a
Darwin MBP.fios-router.home 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64 i386 MacBookPro11,2 Darwin

$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

Upvotes: 1

Views: 264

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626804

You need to access the capture groups inside the match that you obtain with regex_match function. Access each of the capture group by the numeric index:

std::cout << ( *it )[1] << "\n" << ( *it )[2]<< "\n" 
                                << ( *it )[3] << "\n" << ( *it )[4] << std::endl;

See IDEONE demo

Also, please note the raw string literal regex declaration:

std::regex pat{ R"(([a-zA-Z0-9]{1,6})\s*(\d{6})([CP]{1})(\d{8}))" };
                ^^^                                            ^^ 

Upvotes: 4

Related Questions