std::regex

C++ 2012. 7. 5. 17:04

#include "stdafx.h"


#include <string>

#include <vector>

#include <iostream>

#include <regex>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])

{

if (false) {

const std::string input = "Hello World";

const std::regex regex("(.*) (.*)");


std::smatch match; // std::match_result ready templated for std::string


if (std::regex_match(input, match, regex)) {

std::cout << "full match " << match[0] << std::endl;

std::cout << "first group " << match[1] << 

" beginning at: " << match[1].first - input.begin() << std::endl;

std::cout << "second group " << match[2] << 

" beginning at: " << match[2].first - input.begin() << std::endl;

}

}

if (false) {

const string input = "I want to get all the o's all";

const std::regex regex("all");

std::smatch match;


cout << input << endl;


// "all" 이 시작되는 위치

for (std::sregex_iterator it(input.begin(), input.end(), regex), end; it != end; ++it) {

std::cout << "Found at: " << it->position() << std::endl;

}


const string replace_str = "ALL";

string replaced = std::regex_replace(input, regex, replace_str, std::regex_constants::match_default);

cout << replaced << endl;

}

if (false) {

const string input = " <html> hohoaan <tag1> <ttt <tag2>";

const std::regex regex("<[^>]+>");

std::smatch match;


cout << input << endl;


const string replace_str = " TAG ";

string replaced = std::regex_replace(input, regex, replace_str, std::regex_constants::match_default);

cout << replaced << endl;

}


if (true) { // ${} 를 __VAR__ 로 변경

const string input = " ${HI} a ${BMADM} b${} c";

const std::regex regex("\\$\\{[^\\}]*\\}");

std::smatch match;


cout << input << endl;


{

const string replace_str = "__VAR__";

string replaced = std::regex_replace(input, regex, replace_str, std::regex_constants::match_default);

cout << replaced << endl;

}


{

std::sregex_iterator it(input.begin(), input.end(), regex);

std::sregex_iterator end;

for (; it != end; ++it) {

size_t length = it->length(); // 찾은 문자 전체 길이

size_t position = it->position(); // 찾은 위치

string str = it->str(); // 찾은 문자들

string suffix = it->suffix(); // 나머지 문자들

bool empty = it->empty();

}

}


{

std::sregex_token_iterator it(input.begin(), input.end(), regex);

std::sregex_token_iterator end;

for (; it != end; ++it) {

size_t length = it->length(); // 찾은 문자 전체 길이

it->first;

it->second;

string str = it->str(); // 찾은 문자들

}

}

}

string in;

cin >> in;

return 0;

}


'C++' 카테고리의 다른 글

wxZip 사용  (0) 2012.07.24
wxRichTextCtrl 사용방법  (0) 2012.07.14
wxColour #FF000000 - 알파 인식여부 확인  (0) 2012.07.11
wxRegEx 사용하여 특정 문자 치환  (0) 2012.07.11
atlconv.h code page 관련  (0) 2012.07.04
shorcut 바로가기 아이콘 만들기  (0) 2012.06.30
c++ cookie 읽기  (0) 2012.06.26
7z LZMA sdk 윈도우 visual studio 컴파일  (0) 2012.06.26