C++入門/正規表現2
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
''[[FrontPage]]''
* C++と正規表現(regex++) テストその2 [#j066e05d]
C/C++言語ソースファイルを想定し、正規表現を使って解析する...
[[C++入門/正規表現1]]~
[[C++入門/正規表現3]]
** 制約 [#x3955e7b]
MFC、.NET関数を使用しないこと。~
~
#include <string> ...
#include <iostream> ...
#include <boost/regex.hpp> ...
#include <tchar.h> ...
// # include "x.x.h" ...
...
using namespace std; ...
using namespace boost; ...
...
#ifdef UNICODE ...
typedef wregex tregex; ...
typedef wstring tstring; ...
typedef wsmatch tsmatch; ...
//typedef wprintf tprintf; ...
#else ...
typedef regex tregex; ...
typedef string tstring; ...
typedef smatch tsmatch; ...
//typedef wprintf tprintf; ...
#endif ...
...
tregex targetI( ...
_T("#[[:space:]]*(include)") ...
_T("[[:space:]]*L*\"") ...
_T("(.*?") ...
_T("\.[hc]p{0,2})") ...
_T("\"" )); ...
...
char main( void ) ...
{ ...
const tstring file( ...
_T("#include <windows.h> \n") ...
_T("#include \"me.h.h\" \n") ...
_T("#include \"stuff.cpp\"")); ...
...
tstring::const_iterator start; ...
tstring::const_iterator end; ...
...
start = file.begin(); ...
end = file.end(); ...
...
tsmatch partial; ...
match_flag_type flags = match_default; ...
...
while(regex_search(start, end, partial, targetI, fla...
{ ...
printf( "partial[0] = %s\n", ...
string( partial[0].first, partial[0].second)...
wprintf( L"partial[1] = %s\n", ...
wstring( partial[1].first, partial[1].second...
wprintf( L"partial[2] = %s\n", ...
wstring( partial[2].first, partial[2].second...
wprintf( L"partial[3] = %s\n", ...
wstring( partial[3].first, partial[3].second...
wprintf( L"partial[4] = %s\n\n", ...
wstring( partial[4].first, partial[4].second...
...
start = partial[0].second; ...
...
flags |= match_prev_avail; ...
flags |= match_not_bob; ...
} ...
...
getchar(); ...
return 0; ...
}
表示結果
partial[0] = #include "me.h.h"
partial[1] = include
partial[2] = me.h.h
partial[3] =
partial[4] =
partial[0] = #include "stuff.cpp"
partial[1] = include
partial[2] = stuff.cpp
partial[3] =
partial[4] =
*** 修正履歴 (その1からの分も含む) [#o388b8f2]
boost、regexライブラリのビルドに成功。~
Unicodeへの対応は強引だが可能?(なにがおもしろいのかがい...
*** 課題 [#ze539c80]
インクルードファイル名が"xxxx.h.h"のように、複数のピリオ...
defineによる定数定義が、文字定数によって行われていたら?(...
#define CHAR_THREE '3' // ソースとしては問題のない...
今はcoutで画面表示に成功しているが、UNICODE対応(wsmatch使...
むしろ、sub_matchが文字列として表示されていることが奇跡的...
string( partial[0].first, partial[0].second).c_str() ) ...
終了行:
''[[FrontPage]]''
* C++と正規表現(regex++) テストその2 [#j066e05d]
C/C++言語ソースファイルを想定し、正規表現を使って解析する...
[[C++入門/正規表現1]]~
[[C++入門/正規表現3]]
** 制約 [#x3955e7b]
MFC、.NET関数を使用しないこと。~
~
#include <string> ...
#include <iostream> ...
#include <boost/regex.hpp> ...
#include <tchar.h> ...
// # include "x.x.h" ...
...
using namespace std; ...
using namespace boost; ...
...
#ifdef UNICODE ...
typedef wregex tregex; ...
typedef wstring tstring; ...
typedef wsmatch tsmatch; ...
//typedef wprintf tprintf; ...
#else ...
typedef regex tregex; ...
typedef string tstring; ...
typedef smatch tsmatch; ...
//typedef wprintf tprintf; ...
#endif ...
...
tregex targetI( ...
_T("#[[:space:]]*(include)") ...
_T("[[:space:]]*L*\"") ...
_T("(.*?") ...
_T("\.[hc]p{0,2})") ...
_T("\"" )); ...
...
char main( void ) ...
{ ...
const tstring file( ...
_T("#include <windows.h> \n") ...
_T("#include \"me.h.h\" \n") ...
_T("#include \"stuff.cpp\"")); ...
...
tstring::const_iterator start; ...
tstring::const_iterator end; ...
...
start = file.begin(); ...
end = file.end(); ...
...
tsmatch partial; ...
match_flag_type flags = match_default; ...
...
while(regex_search(start, end, partial, targetI, fla...
{ ...
printf( "partial[0] = %s\n", ...
string( partial[0].first, partial[0].second)...
wprintf( L"partial[1] = %s\n", ...
wstring( partial[1].first, partial[1].second...
wprintf( L"partial[2] = %s\n", ...
wstring( partial[2].first, partial[2].second...
wprintf( L"partial[3] = %s\n", ...
wstring( partial[3].first, partial[3].second...
wprintf( L"partial[4] = %s\n\n", ...
wstring( partial[4].first, partial[4].second...
...
start = partial[0].second; ...
...
flags |= match_prev_avail; ...
flags |= match_not_bob; ...
} ...
...
getchar(); ...
return 0; ...
}
表示結果
partial[0] = #include "me.h.h"
partial[1] = include
partial[2] = me.h.h
partial[3] =
partial[4] =
partial[0] = #include "stuff.cpp"
partial[1] = include
partial[2] = stuff.cpp
partial[3] =
partial[4] =
*** 修正履歴 (その1からの分も含む) [#o388b8f2]
boost、regexライブラリのビルドに成功。~
Unicodeへの対応は強引だが可能?(なにがおもしろいのかがい...
*** 課題 [#ze539c80]
インクルードファイル名が"xxxx.h.h"のように、複数のピリオ...
defineによる定数定義が、文字定数によって行われていたら?(...
#define CHAR_THREE '3' // ソースとしては問題のない...
今はcoutで画面表示に成功しているが、UNICODE対応(wsmatch使...
むしろ、sub_matchが文字列として表示されていることが奇跡的...
string( partial[0].first, partial[0].second).c_str() ) ...
ページ名: