Show toolbar

2014年7月14日 星期一

Regular Expression Iterator

標題:使用迭代器匹配字串
VC++ (regex.cpp):
#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;
}


說明:
使用迭代器匹配字串。

沒有留言:

張貼留言