Show toolbar

2014年7月14日 星期一

Regular Expression to match IP Address

標題:使用正規表示法驗證IP
VC++ (regex.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <regex> //string lib here
using namespace std;
int main(void) {
string ip = "192.168.1.100";
smatch results;
regex pattern("([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)");
if(regex_match(ip, results, pattern)) {
cout << atof( results[1].str().c_str() ) << endl;
cout << atof( results[2].str().c_str() ) << endl;
cout << atof( results[3].str().c_str() ) << endl;
cout << atof( results[4].str().c_str() ) << endl;
} else {
cout << "Not Match." << endl;
}
system("pause");
return 0;
}

說明:
簡單使用正規表示法驗證IP,並印出每個區段數值。

沒有留言:

張貼留言