{"id":335,"date":"2014-08-11T23:58:34","date_gmt":"2014-08-12T08:58:34","guid":{"rendered":"http:\/\/blog.box.kr\/?p=335"},"modified":"2014-08-11T23:58:34","modified_gmt":"2014-08-12T08:58:34","slug":"%ec%a0%95%ea%b7%9c%ed%91%9c%ed%98%84%ec%8b%9dregular-expression-on-c","status":"publish","type":"post","link":"https:\/\/blog.box.kr\/?p=335","title":{"rendered":"\uc815\uaddc\ud45c\ud604\uc2dd(Regular Expression) on C++"},"content":{"rendered":"<p>*\u00a0C++ Regex<\/p>\n<p>2011\ub144 \uacbd C++\uc758 \uc0c8\ub85c\uc6b4 \ud45c\uc900 C++11 (C++0x)\uc744 \uc815\uc758\ud558\ub294 \uacfc\uc815\uc5d0\uc11c Boost.Regex\uac00 \ud45c\uc900\uc73c\ub85c \ud3ec\ud568\ub418\uc5c8\ub2e4. C++11\uc758 \uc815\uaddc\ud45c\ud604\uc2dd \ub77c\uc774\ube0c\ub7ec\ub9ac\ub294 &lt;regex&gt; \ud5e4\ub354\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4.\uc774\ub85c \uc778\ud574 \ubcc4\ub3c4\uc758 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc124\uce58\ud558\uc9c0 \uc54a\uace0\ub3c4 \uc190\uc27d\uac8c \uc815\uaddc\ud45c\ud604\uc2dd \uae30\ub2a5\uc744 \ud65c\uc6a9\ud560 \uc218 \uc788\uac8c \ub418\uc5c8\ub2e4. \ucd5c\uc2e0 \ubc84\uc804\uc758 Visual C++, GNU C++ \ucef4\ud30c\uc77c\ub7ec\ub294 std::regex\ub97c \ud3ec\ud568\ud55c C++11 \ud45c\uc900\uc774 \uc815\uc758\ud55c \ub300\ubd80\ubd84\uc758 \uae30\ub2a5\uc744 \uc9c0\uc6d0\ud55c\ub2e4. \ucef4\ud30c\uc77c\ub7ec\ubcc4 C++11 \ud45c\uc900 \uc9c0\uc6d0\uc5d0 \ub300\ud55c \uc790\uc138\ud55c \ub9ac\uc2a4\ud2b8\ub294 C++0xCompilerSupport (\uc601\ubb38) \ubb38\uc11c\ub97c \ucc38\uace0.<\/p>\n<p>VS 2012: \uc9c0\uc6d0<br \/>\nGCC \ubc84\uc804 4.8: \ubbf8\uc9c0\uc6d0<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\uc608\uc81c<br \/>\nstd::regex\ub97c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c\ub294 \uba3c\uc800 \uc815\uaddc\ud45c\ud604\uc2dd \ubb38\uc790\uc5f4\uc744 \uc778\uc790\ub85c std::regex \uac1d\uccb4\ub97c \uc0dd\uc131\ud55c \ub4a4, \uac80\uc0ac\ud558\uace0\uc790 \ud558\ub294 \ubb38\uc790\uc5f4\uc774 \uc815\uaddc\ud45c\ud604\uc2dd\uc5d0 \uc815\ud655\ud788 \uc77c\uce58\ud558\ub294\uc9c0\ub97c \uc54c\uc544\ubcfc \ub54c\ub294 std::regex_match, \ubb38\uc790\uc5f4\uc774 \uc815\uaddc\ud45c\ud604\uc2dd\uc5d0 \ubd80\ud569\ub418\ub294 \ubd80\ubd84\uc744 \ud3ec\ud568\ud558\uace0 \uc788\ub294\uc9c0\ub97c \uc54c\uc544\ubcfc \ub54c\ub294 std::regex_search\ub97c \uc0ac\uc6a9\ud558\uba74 \ub41c\ub2e4.<br \/>\n\ub2e4\uc74c\uc740 \uc785\ub825\ubc1b\uc740 \uac12\uc774 &#8220;hello&#8221;\uc778\uc9c0\ub97c \ud655\uc778\ud558\ub294 \ucf54\ub4dc\uc774\ub2e4.<br \/>\n\/\/ \uc774 \ucf54\ub4dc\ub294 regex \uc0ac\uc6a9\uc744 \ubcf4\uc5ec\uc8fc\uae30 \uc704\ud55c \uc608\uc81c\uc77c \ubfd0,<br \/>\n\/\/ \ub2e8\uc21c \ubb38\uc790\uc5f4 \ube44\uad50\uc758 \uacbd\uc6b0\uc5d4 strcmp \uac19\uc740 CRT \ud568\uc218\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \ub0ab\uc2b5\ub2c8\ub2e4.<br \/>\n#include &lt;regex&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\nstd::regex rx(&#8220;hello&#8221;);<br \/>\nstd::regex exit(&#8220;exit&#8221;);<br \/>\nstd::string s;<\/p>\n<p>std::getline(std::cin, s);<br \/>\nwhile(false == std::regex_match(s, exit))<br \/>\n{<br \/>\nif(std::regex_match(s, rx))<br \/>\nstd::endl(std::cout &lt;&lt; &#8220;match&#8221;);<br \/>\nelse<br \/>\nstd::endl(std::cout &lt;&lt; &#8220;mismatch&#8221;);<\/p>\n<p>std::getline(std::cin, s);<br \/>\n}<br \/>\n}<br \/>\n\ub2e4\uc74c\uc740 ID \uac12\uc774 \uc601\ubb38 \ub610\ub294 \uc22b\uc790 4~20 \uae00\uc790\uc5d0 \ud574\ub2f9\ud558\ub294\uc9c0 \ud655\uc778\ud558\ub294 \ucf54\ub4dc\uc774\ub2e4.<br \/>\n#include &lt;regex&gt;<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\nstd::regex rx(&#8220;^[a-z0-9A-Z]{4,20}+$&#8221;);<br \/>\nstd::regex exit(&#8220;exit&#8221;);<br \/>\nstd::string s;<\/p>\n<p>std::getline(std::cin, s);<br \/>\nwhile(false == std::regex_match(s, exit))<br \/>\n{<br \/>\nif(std::regex_match(s, rx))<br \/>\nstd::endl(std::cout &lt;&lt; &#8220;valid&#8221;);<br \/>\nelse<br \/>\nstd::endl(std::cout &lt;&lt; &#8220;invalid&#8221;);<\/p>\n<p>std::getline(std::cin, s);<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>\ub9cc\uc57d \uac80\uc0c9\ud55c \uae00\uc790\ub97c \ud3ec\ud568\ud558\uc5ec \ubb54\uac00\ub97c \ucd94\uac00 \ud574\uc11c replace\ud558\uace0 \uc2f6\ub2e4\uba74?<br \/>\n\uc989, [0-9].bmp \ub77c\ub294 \ud328\ud134\uc744 \ucc3e\uc544\uc11c d:\/[0-9].bmp\ub85c \ubc14\uafb8\uace0 \uc2f6\ub2e4\uba74??<\/p>\n<p>\uadf8\ub7f4 \ub54c\ub294 ..<\/p>\n<p>std::regex rx(&#8220;([0-9]-[0-9].bmp)&#8221;);<\/p>\n<p>\uc774\ub807\uac8c \ub9cc\ub4e4\uc5b4 \ub193\uc73c\uba74 $1, $2 \uc774\ub7f0\uc2dd\uc73c\ub85c \u00a0( \uad04\ud638\ub85c \ubb36\uc740 \ud328\ud134 \uac2f\uc218 \ub9cc\ud07c 1 \uc5d0\uc11c n\uac1c\ub85c \ucee4\uc838\uac04\ub2e4.. )<br \/>\n\uc0ac\uc6a9\uc774 \uac00\ub2a5<\/p>\n<p>\uc989..<br \/>\nstd::string szReplacedData = std::regex_replace(szPrintData.c_str(), rx, \u00a0&#8221; d:\/$1&#8243;);<br \/>\n\ub85c \uad50\uccb4\uac00 \uac00\ub2a5 \ud568.<\/p>\n<p>&nbsp;<\/p>\n<p>\uc88b\ub124.. \uc815\uaddc\uc2dd.. \ub178\uac00\ub2e4\uac00 \uc904\uc5c8\ub2e4..<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>*\u00a0C++ Regex 2011\ub144 \uacbd C++\uc758 \uc0c8\ub85c\uc6b4 \ud45c\uc900 C++11 (C++0x)\uc744 \uc815\uc758\ud558\ub294 \uacfc\uc815\uc5d0\uc11c Boost.Regex\uac00 \ud45c\uc900\uc73c\ub85c \ud3ec\ud568\ub418\uc5c8\ub2e4. C++11\uc758 \uc815\uaddc\ud45c\ud604\uc2dd \ub77c\uc774\ube0c\ub7ec\ub9ac\ub294 &lt;regex&gt; \ud5e4\ub354\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4.\uc774\ub85c \uc778\ud574 \ubcc4\ub3c4\uc758 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc124\uce58\ud558\uc9c0 \uc54a\uace0\ub3c4 \uc190\uc27d\uac8c \uc815\uaddc\ud45c\ud604\uc2dd \uae30\ub2a5\uc744 \ud65c\uc6a9\ud560 \uc218 \uc788\uac8c \ub418\uc5c8\ub2e4. \ucd5c\uc2e0 \ubc84\uc804\uc758 Visual C++, GNU C++ \ucef4\ud30c\uc77c\ub7ec\ub294 std::regex\ub97c \ud3ec\ud568\ud55c C++11 \ud45c\uc900\uc774 \uc815\uc758\ud55c \ub300\ubd80\ubd84\uc758 \uae30\ub2a5\uc744 \uc9c0\uc6d0\ud55c\ub2e4. \ucef4\ud30c\uc77c\ub7ec\ubcc4 C++11 \ud45c\uc900 \uc9c0\uc6d0\uc5d0 \ub300\ud55c \uc790\uc138\ud55c \ub9ac\uc2a4\ud2b8\ub294 C++0xCompilerSupport (\uc601\ubb38) \ubb38\uc11c\ub97c \ucc38\uace0. VS 2012: \uc9c0\uc6d0 GCC \ubc84\uc804 4.8: \ubbf8\uc9c0\uc6d0 &nbsp; &nbsp; \uc608\uc81c std::regex\ub97c \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c\ub294 \uba3c\uc800 \uc815\uaddc\ud45c\ud604\uc2dd \ubb38\uc790\uc5f4\uc744 \uc778\uc790\ub85c std::regex \uac1d\uccb4\ub97c \uc0dd\uc131\ud55c \ub4a4, \uac80\uc0ac\ud558\uace0\uc790 \ud558\ub294 \ubb38\uc790\uc5f4\uc774 \uc815\uaddc\ud45c\ud604\uc2dd\uc5d0 \uc815\ud655\ud788 \uc77c\uce58\ud558\ub294\uc9c0\ub97c \uc54c\uc544\ubcfc \ub54c\ub294 std::regex_match, \ubb38\uc790\uc5f4\uc774 \uc815\uaddc\ud45c\ud604\uc2dd\uc5d0 \ubd80\ud569\ub418\ub294 \ubd80\ubd84\uc744 \ud3ec\ud568\ud558\uace0 \uc788\ub294\uc9c0\ub97c \uc54c\uc544\ubcfc \ub54c\ub294 std::regex_search\ub97c \uc0ac\uc6a9\ud558\uba74 \ub41c\ub2e4. \ub2e4\uc74c\uc740 \uc785\ub825\ubc1b\uc740 \uac12\uc774 &#8220;hello&#8221;\uc778\uc9c0\ub97c \ud655\uc778\ud558\ub294 \ucf54\ub4dc\uc774\ub2e4. \/\/ \uc774 \ucf54\ub4dc\ub294 regex \uc0ac\uc6a9\uc744 \ubcf4\uc5ec\uc8fc\uae30 \uc704\ud55c \uc608\uc81c\uc77c \ubfd0, \/\/ \ub2e8\uc21c \ubb38\uc790\uc5f4 \ube44\uad50\uc758 \uacbd\uc6b0\uc5d4 strcmp \uac19\uc740 CRT \ud568\uc218\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \ub0ab\uc2b5\ub2c8\ub2e4. #include &lt;regex&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main() { std::regex rx(&#8220;hello&#8221;); std::regex exit(&#8220;exit&#8221;); std::string s; std::getline(std::cin, s); while(false == std::regex_match(s, exit)) { if(std::regex_match(s, rx)) std::endl(std::cout &lt;&lt; &#8220;match&#8221;); else std::endl(std::cout &lt;&lt; &#8220;mismatch&#8221;); std::getline(std::cin, s); } } \ub2e4\uc74c\uc740 ID \uac12\uc774 \uc601\ubb38 \ub610\ub294 \uc22b\uc790 4~20 \uae00\uc790\uc5d0 \ud574\ub2f9\ud558\ub294\uc9c0 \ud655\uc778\ud558\ub294 \ucf54\ub4dc\uc774\ub2e4. #include &lt;regex&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main() { std::regex rx(&#8220;^[a-z0-9A-Z]{4,20}+$&#8221;); std::regex exit(&#8220;exit&#8221;); std::string s; std::getline(std::cin, s); while(false == std::regex_match(s, exit)) { if(std::regex_match(s, rx)) std::endl(std::cout &lt;&lt; &#8220;valid&#8221;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"ngg_post_thumbnail":0,"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[9],"tags":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5q9Zn-5p","jetpack-related-posts":[{"id":337,"url":"https:\/\/blog.box.kr\/?p=337","url_meta":{"origin":335,"position":0},"title":"\uc815\uaddc\uc2dd \uc0ac\uc6a9\ubc95 [\ud38c] boost \uc0ac\uc6a9","date":"2014-08-12","format":false,"excerpt":"\uc9c8\ubb38 ::#\uc774 \ubd99\uc740 \uc804\ud654 \ubc88\ud638 \uc55e\uc5d0\ub294 %23\uc744 \ubd99\uc774\uace0 \uc2f6\uc74c.. \u00a0 \ub2f5\ucf54\ub4dc :: std::string result, list = \"#011-222-3333\"; const char* regStr = \"(#)?(01[016789])-?(d{3,4})-?(d{4})\"; const char* subStr = \"(?1%23)$2$3$4\"; boost::regex_replace(result, list.begin(), list.end(), regStr, subStr, boost::match_default | boost::format_all); std::cout << result << std::endl; [url]http:\/\/www.boost.org\/libs\/regex\/doc\/regex_replace.html[\/url] [url]http:\/\/ourworld.compuserve.com\/homepages\/John_Maddock\/[\/url] [url]http:\/\/research.microsoft.com\/projects\/greta\/gretauserguide.htm[\/url] \u00a0","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":339,"url":"https:\/\/blog.box.kr\/?p=339","url_meta":{"origin":335,"position":1},"title":"[c++11] \uc798 \uc4f0\uba74 \ub9e4\uc6b0 \ud3b8\ub9ac\ud55c C++11\uc758 \ubb38\ubc95\ub4e4","date":"2014-08-12","format":false,"excerpt":"C\/C++\uc5b8\uc5b4\ub294 \uc800\uc218\uc900\uae4c\uc9c0 \uc815\ubc00\ud558\uac8c \ub2e4\ub8f0 \uc218 \uc788\ub294 \ubc18\uba74, \ud504\ub85c\uadf8\ub798\uba38\uac00 \uc77c\uc77c\ud788 \uad00\ub9ac\ud574\uc918\uc57c\ud558\ub294 \ubd80\ubd84\uc774 \ub9ce\ub2e4\ub294\uac8c C\/C++\uc5b8\uc5b4\uc5d0 \ub300\ud55c \uc77c\ubc18\uc801\uc778 \uacac\ud574\uc785\ub2c8\ub2e4. \uadf8\ub798\uc11c \uc800\uc218\uc900\uae4c\uc9c0 \uc77c\uc77c\ud788 \uc2e0\uacbd\uc4f0\uc9c0 \uc54a\uc544\ub3c4 \ub418\ub294 \ucfe8\ud55c \uc5b8\uc5b4\ub97c \uac00\uc9c0\uace0 \ub192\uc740 \uc0dd\uc0b0\uc131\uc744 \ucd94\uad6c\ud558\ub294\uac8c \ucd94\uc138\uc774\uae30\ub3c4 \ud569\ub2c8\ub2e4. . . . \ub9cc, C++\uc758 \uace8\uc218\ube60\ub85c\uc11c C++11\uc744 \ud1b5\ud574 C++\ub85c\ub3c4 \ucda9\ubd84\ud788 \ucfe8\ud558\uace0 \uc0dd\uc0b0\uc131 \ub192\uc740 \ucf54-\ub4dc\ub97c \uc791\uc131\ud560\uc218 \uc788\ub2e4\uace0 \ubcc0\ud638\ub97c \ud574\ubcf4\ub824\uace0 \ud569\ub2c8\ub2e4. Visual\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":230,"url":"https:\/\/blog.box.kr\/?p=230","url_meta":{"origin":335,"position":2},"title":"std::string \ub2e4\ub8e8\uae30.. ( \ucc3e\uae30, \uacf5\ubc31\uc81c\uac70 \ub4f1\ub4f1)","date":"2014-07-23","format":false,"excerpt":"\ubb38\uc790\uc5f4 string #include \"StdAfx.h\" #include \"StringTest.h\" #include <algorithm> \/\/transform \uc0ac\uc6a9 #include <functional> \/\/bind1st using std::string; \/\/ \uc774\ub807\uac8c \uc9c0\uc815\ud574\uc900\ub2e4. StringTest::StringTest() { log(\"StringTest \uc0dd\uc131\uc790 \ud638\ucd9c\"); } StringTest::~StringTest(void) { log(\"StringTest \uc18c\uba78\uc790 \ud638\ucd9c\"); } void StringTest::test() { log(\"============ string class ==========\"); \/\/ \uc0dd\uc131\uc790 \u00a0 std::string s0; \/\/ \ube44\uc5b4\uc788\ub294 string std::string s1(\"012345\",3); \/\/ \uacb0\uacfc: 012\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":439,"url":"https:\/\/blog.box.kr\/?p=439","url_meta":{"origin":335,"position":3},"title":"[c++][stl] std::vector Copy \ubc29\ubc95","date":"2014-12-15","format":false,"excerpt":"\/\/ copy() \ub97c \uc774\uc6a9\ud55c \ubcf5\uc0ac \u00a0 destVector.resize((int)(sourceVector.size())); std::copy( sourceVector.begin(), sourceVector.end(), destVector.begin() ); std::copy( sourceVector.begin() + 2, sourceVector.begin() + 5, destVector.begin() ); \/\/ assign() \uc744 \uc774\uc6a9\ud55c \ubcf5\uc0ac\u00a0 \u00a0 destVector.clear(); destVector.assign( sourceVector.begin(), sourceVector.end() ); \/\/ \uc804\uccb4 \ubcf5\uc0ac destVector.assign( sourceVector.begin() + 1, sourceVector.begin() + 4 );","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":178,"url":"https:\/\/blog.box.kr\/?p=178","url_meta":{"origin":335,"position":4},"title":"std::map example","date":"2014-07-09","format":false,"excerpt":"\/\/ std::map example \/\/ opposite words #include <iostream> #include <map> #include <string> using namespace std; typedef std::map<std::string, std::string> TStrStrMap; typedef std::pair<std::string, std::string> TStrStrPair; int main(int argc, char *argv[]) { TStrStrMap tMap; tMap.insert(TStrStrPair(\"yes\", \"no\")); tMap.insert(TStrStrPair(\"up\", \"down\")); tMap.insert(TStrStrPair(\"left\", \"right\")); tMap.insert(TStrStrPair(\"good\", \"bad\")); std::string s; std::cout << \"Enter word: \" << std::endl;; std::cin >>\u2026","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":143,"url":"https:\/\/blog.box.kr\/?p=143","url_meta":{"origin":335,"position":5},"title":"Splitting a C++ std::string using tokens, e.g. \u201c;\u201d [duplicate]","date":"2014-06-29","format":false,"excerpt":"std::vector<std::string> split(const std::string& s,char seperator){ std::vector<std::string> output; std::string::size_type prev_pos =0, pos =0;while((pos = s.find(seperator, pos))!= std::string::npos){ std::string substring( s.substr(prev_pos, pos-prev_pos)); output.push_back(substring); prev_pos =++pos;} output.push_back(s.substr(prev_pos, pos-prev_pos));\/\/ Last wordreturn output;}","rel":"","context":"In &quot;C\/C++&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335"}],"version-history":[{"count":0,"href":"https:\/\/blog.box.kr\/index.php?rest_route=\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.box.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}