VC++ (regex.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <regex> //string lib here using namespace std; int main( int argc, char * argv[]) { string str = "facet normal -8.944272e-001 4.472136e-001 0.000000e+000" ; regex pattern( "[+-]?[0-9]\\.[0-9]+[eE][+-]?[0-9]+" ); auto words_begin = sregex_iterator(str.begin(), str.end(), pattern); auto words_end = sregex_iterator(); cout << "Found " << std::distance(words_begin, words_end) << " numbers:\n" ; for (sregex_iterator i = words_begin; i != words_end; ++i) { cout << atof ( ((smatch) *i).str().c_str() ) << '\n' ; } system ( "pause" ); return 0; } |
說明:
使用迭代器匹配字串。