티스토리 뷰

Code/C C++

[C++] Regular Expressions Class: regex

Hide Code 2010. 4. 18. 16:50

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <regex>

#ifdef _UNICODE
  #define tcout wcout
  #define tcin wcin
  #define tstring wstring
#else
  #define tcout cout
  #define tcin cin
  #define tstring string
#endif

#ifdef _UNICODE
  #define tregex wregex
  #define tcmatch wcmatch
  #define tsmatch wsmatch
#else
  #define tregex regex
  #define tcmatch cmatch
  #define tsmatch smatch
#endif

using namespace std;

void method1();
void method2();
void method3();
void method4();
void method5();

int _tmain(int argc, _TCHAR* argv[])
{
    method1();
    method2();
    method3();
    method4();
    method5();

    getchar();
    return 0;
}

void method1()
{
    tstring sourceString = _T("abc123");
    tstring patternString = _T("c1");
    tregex regexPattern(patternString);
    tcout << regex_match(sourceString, regexPattern) << endl;  // prints 0 (false)
    tcout << regex_search(sourceString, regexPattern) << endl; // prints 1 (true)
}

void method2()
{
    tstring sourceString = _T("abc123def456");
    tstring patternString = _T("(\\d+)");
    tstring replaceString = _T("<$1>");
    tregex regexPattern(patternString);
    tcout << regex_replace(sourceString, regexPattern, replaceString) << endl;  // prints abc<123>def<456>
}

void method3()
{
    tstring sourceString = _T("1:22:33:444");
    tregex regexPattern(_T("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"));
    tsmatch matchResults;

    bool valid = regex_match(sourceString, matchResults, regexPattern);
    tcout << sourceString.c_str() << _T(" : ") << (valid ? _T("valid") : _T("invalid")) << endl;

    if(valid)
    {
        tcout << "b1: " << matchResults[1] << endl; // prints 1
        tcout << "b2: " << matchResults[2] << endl; // prints 22
        tcout << "b3: " << matchResults[3] << endl; // prints 33
        tcout << "b4: " << matchResults[4] << endl; // prints 444
    }
}

void method4()
{
    tstring sourceString = _T("1:22:33:444");
    tregex regexPattern(_T("(\\d+):(\\d+)"));
    tsmatch matchResults;

    bool valid = regex_search(sourceString, matchResults, regexPattern);
    tcout << sourceString.c_str() << _T(" : ") << (valid ? _T("valid") : _T("invalid")) << endl;

    if(valid)
    {
        tcout << "b1: " << matchResults[1] << endl;
        tcout << "b2: " << matchResults[2] << endl;
    }
}

void method5()
{
    tstring sourceString = _T("1:22 33:444 5:66 777:88");
    tregex regexPattern(_T("(\\d+):(\\d+)"));
    tsmatch matchResults;
    tstring::const_iterator start = sourceString.begin();
    tstring::const_iterator close = sourceString.end();

    while(regex_search(start, close, matchResults, regexPattern))
    {
        tcout << "b1: " << matchResults[1] << endl;
        tcout << "b2: " << matchResults[2] << endl;
        start = matchResults[0].second;
    }
}



#pragma once

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <boost/regex.hpp>

#ifdef _UNICODE
#define tcout wcout
#define tcin wcin
#define tstring wstring
#else
#define tcout cout
#define tcin cin
#define tstring string
#endif

#ifdef _UNICODE
#define tregex wregex
#define tcmatch wcmatch
#define tsmatch wsmatch
#else
#define tregex regex
#define tcmatch cmatch
#define tsmatch smatch
#endif

using namespace std;

void method1();
void method2();
void method3();
void method4();
void method5();

int _tmain(int argc, _TCHAR* argv[])
{
    method1();
    method2();
    method3();
    method4();
    method5();

    getchar();
    return 0;
}

void method1()
{
    tstring sourceString = _T("abc123");
    tstring patternString = _T("c1");
    boost::tregex regexPattern(patternString);
    tcout << boost::regex_match(sourceString, regexPattern) << endl;  // prints 0 (false)
    tcout << boost::regex_search(sourceString, regexPattern) << endl; // prints 1 (true)
}

void method2()
{
    tstring sourceString = _T("abc123def456");
    tstring patternString = _T("(\\d+)");
    tstring replaceString = _T("<$1>");
    boost::tregex regexPattern(patternString);
    tcout << boost::regex_replace(sourceString, regexPattern, replaceString) << endl;  // prints abc<123>def<456>
}

void method3()
{
    tstring sourceString = _T("1:22:33:444");
    boost::tregex regexPattern(_T("(\\d{1,3}):(\\d{1,3}):(\\d{1,3}):(\\d{1,3})"));
    boost::tsmatch matchResults;

    bool valid = boost::regex_match(sourceString, matchResults, regexPattern);
    tcout << sourceString.c_str() << _T(" : ") << (valid ? _T("valid") : _T("invalid")) << endl;

    if(valid)
    {
        tcout << "b1: " << matchResults[1] << endl; // prints 1
        tcout << "b2: " << matchResults[2] << endl; // prints 22
        tcout << "b3: " << matchResults[3] << endl; // prints 33
        tcout << "b4: " << matchResults[4] << endl; // prints 444
    }
}

void method4()
{
    tstring sourceString = _T("1:22:33:444");
    boost::tregex regexPattern(_T("(\\d+):(\\d+)"));
    boost::tsmatch matchResults;

    bool valid = boost::regex_search(sourceString, matchResults, regexPattern);
    tcout << sourceString.c_str() << _T(" : ") << (valid ? _T("valid") : _T("invalid")) << endl;

    if(valid)
    {
        tcout << "b1: " << matchResults[1] << endl;
        tcout << "b2: " << matchResults[2] << endl;
    }
}

void method5()
{
    tstring sourceString = _T("1:22 33:444 5:66 777:88");
    boost::tregex regexPattern(_T("(\\d+):(\\d+)"));
    boost::tsmatch matchResults;
    tstring::const_iterator start = sourceString.begin();
    tstring::const_iterator close = sourceString.end();

    while(boost::regex_search(start, close, matchResults, regexPattern))
    {
        tcout << "b1: " << matchResults[1] << endl;
        tcout << "b2: " << matchResults[2] << endl;
        start = matchResults[0].second;
    }
}


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함