VC++ (main.cpp):
#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++中使用正規表示式取得數值。
沒有留言:
張貼留言