Last active
May 30, 2017 08:43
-
-
Save LusainKim/43ca9a8884dff63f3c72701a1736f57d to your computer and use it in GitHub Desktop.
문자열로 입력받은 IP 주소가 유효한지 정규식을 사용하여 검사하는 코드입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <regex> | |
using namespace std; | |
int main() | |
{ | |
regex rg { "^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$" }; | |
string input; | |
while (true) | |
{ | |
getline(cin, input); | |
smatch m; | |
if (regex_search(input, m, rg)) | |
{ | |
int elem[4] { stoi(m[1]) , stoi(m[2]) , stoi(m[3]) , stoi(m[4]) }; | |
for (auto p : elem) | |
{ | |
if (!(0 <= p && p < 256)) | |
goto nomatch; | |
} | |
cout << "match!" << endl; | |
} | |
else | |
{ | |
nomatch: | |
cout << "no match..." << endl; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
제가 사용한 정규표현식은 조금 다른데 참고가 되었으면 합니다. PCRE 기준입니다.