わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

wstringを正規表現で検索する

std::tr1::regex_searchを使って正規表現で検索を行います。

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



int _tmain
(
	  int argc
	, _TCHAR* argv[]
)
{
	// 標準出力にユニコード出力する
	setlocale( LC_ALL, "Japanese" );

	try {

		std::wstring strRegEx = L"(.*)";
		std::wcout << L"■検索文字列(正規表現)" << std::endl;
		std::wcout << strRegEx << std::endl;
		std::wcout << std::endl;

		// 検索文字列(正規表現)
		std::tr1::basic_regex<TCHAR> oRegexString( strRegEx );

		// 検索対象
		std::wstring str = L"あいうえお(かきくけこ)";
		std::wcout << L"■検索対象" << std::endl;
		std::wcout << str << std::endl;
		std::wcout << std::endl;

		// 検索結果
		std::tr1::match_results<std::wstring::const_iterator> oMatchString;

		// 正規表現で検索
		bool bMatch = std::tr1::regex_search( str, oMatchString, oRegexString );
		if ( bMatch ) {
			std::wcout << L"正規表現に一致しました" << std::endl;
		}
	}
	catch( std::exception& ex ) {
//		WCOUT << L"例外!! = " << endl;
	}

	// 正常終了
	return( 0 );
}

実行結果

■検索文字列(正規表現)
(.*)

■検索対象
あいうえお(かきくけこ)

正規表現に一致しました






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。