C++ c++;11标准::正则表达式错误?
我尝试使用clang在OSX上使用c++11中的正则表达式库:C++ c++;11标准::正则表达式错误?,c++,regex,c++11,C++,Regex,C++11,我尝试使用clang在OSX上使用c++11中的正则表达式库: // 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>
// 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;
}
}
相反,它吐了出来
AAPL
150918
C00099
500
这似乎是一个错误。。。
有人知道解决这个问题的办法吗
谢谢
系统详细信息:
$ 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
您需要访问使用regex\u match
函数获得的匹配中的捕获组。通过数字索引访问每个捕获组:
std::cout << ( *it )[1] << "\n" << ( *it )[2]<< "\n"
<< ( *it )[3] << "\n" << ( *it )[4] << std::endl;
您需要访问使用regex\u match
函数获得的匹配中的捕获组。通过数字索引访问每个捕获组:
std::cout << ( *it )[1] << "\n" << ( *it )[2]<< "\n"
<< ( *it )[3] << "\n" << ( *it )[4] << std::endl;
parseProductString
是否应该是main
?parseProductString
是否应该是main
?我认为您不需要sregex\u迭代器,您可以迭代匹配结果。可能更惯用。但是+1为我舀:)很高兴我能帮上忙。如果它对你有用的话,请考虑接受这个答案。我认为你不需要SReExExx迭代器,你可以迭代匹配结果。可能更惯用。但是+1为我舀:)很高兴我能帮上忙。如果它对你有用的话,请考虑接受这个答案。
std::regex pat{ R"(([a-zA-Z0-9]{1,6})\s*(\d{6})([CP]{1})(\d{8}))" };
^^^ ^^