VC++ (main.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include "stdafx.h" #include <iostream> #include <regex> //string lib here using namespace std; int _tmain( int argc, _TCHAR* argv[]) { cout << "--- Example 1 ---" << endl; //String Match string strA = "13393774" ; smatch matchA; regex regA( "[0-9]+" ); if (regex_match(strA, matchA, regA)) { cout << atof (matchA[0].str().c_str()) << " is number." << endl; } else { cout << "Not a number." << endl; } cout << "--- Example 2 ---" << endl; //String Match string strB = "-4.472136e-001" ; smatch matchB; regex regB( "[+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?" ); if (regex_match(strB, matchB, regB)) { cout << atof (matchB[0].str().c_str()) << " is number." << endl; } else { cout << "Not a number." << endl; } cout << "\n--- Example 3 ---" << endl; //String Search string strC = " facet normal 0.000000e+000 -4.472136e-001 8.944272e-001" ; smatch matchC; regex regC(" *facet normal \ ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \ ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \ ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)"); if (regex_match(strC, matchC, regC)) { cout << "Facet normal: (" ; cout << atof (matchC[1].str().c_str()) << ", " ; cout << atof (matchC[2].str().c_str()) << ", " ; cout << atof (matchC[3].str().c_str()) << ")" << endl; } else { cout << "Not Match." << endl; } return 0; } |
說明:
在C/C++中使用正規表示式取得數值。
沒有留言:
張貼留言