프로그래밍 언어/C++
regex 정규필터식 간단 구문 예제
copynull
2014. 1. 16. 00:57
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 // ConsoleApplication1.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.//#include "stdafx.h"#include// regex matchbool match_regex(TCHAR* strData, TCHAR* pattern){#ifdef _UNICODEwstring data(strData);wsmatch regexMatchResult;wregex mPattern(pattern);#elsestring data(strData);smatch regexMatchResult;regex mPattern(pattern);#endifreturn regex_match(data, regexMatchResult, mPattern);}int _tmain(int argc, _TCHAR* argv[]){// 이름, 알파벳, 숫자, 특수문자if (match_regex(_T("korea"), _T("(([가-힣]|[a-zA-Z0-9_]|[\\-\\[\\]\\(\\)\\{\\}])+)")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;// 주민 번호 (6-7)if (match_regex(_T("881122-1234567"), _T("\\d{6}\\-\\d{7}")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;// URLif (match_regex(_T("http://copynull.tistory.com"), _T("(ftp|http|https):\\/\\/(\\w+)(\\.\\w+)*(\\/([\\w\\d])+\\/{0,1})*")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;// Network Interface MAC addressif (match_regex(_T("ab-21-33-f5-c6-07"), _T("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;// E-Mail Addressif (match_regex(_T("copynull@nate.com"), _T("[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*[.][a-zA-Z]{2,3}")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;// IP Addressif (match_regex(_T("255.255.255.0"), _T("([1]?\\d{1,2}|[2][0-4]\\d|25[0-5])[.]([1]?\\d{1,2}|[2][0-4]\\d|25[0-5])[.]([1]?\\d{1,2}|[2][0-4]\\d|25[0-5])[.]([1]?\\d{1,2}|[2][0-4]\\d|25[0-5])")))cout << "TRUE" << endl;elsecout << "FALSE" << endl;return 0;}