Delphi / C++Builderには、TRegEx, TPerlRegExの正規表現があります。
これは、PCRE ライブラリをラップし Delphiで使えるようにしたものです。
TPerlRegEx は、Perl-Compatible 正規表現を実装します。
C++Builderを利用すると、RTLとC++標準ライブラリ両方が利用できます、regexも利用可能です。
両方の良い部分が利用できますので、実装の幅が広がります。
C++Builder 10.2.2 Tokyo ではC++11の機能が使えます。
ターゲットデバイスをiOS(iPhone X)にし、std::regexを C++Builder 10.2で利用して
数字、平仮名、ASCII、メアド以上4項目のパターンマッチを試してみました。
![]()
コード
#include <string>
#include <regex>
#include <functional>
#include <vector>
#include <tuple>
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
std::vector<std::tuple<std::wstring, std::wstring> > v1{
{L"\\d+", L"regex_match(\"\\d+\",%s)"},
{L"^[\u3040-\u309F]+$", L"regex_match(\"^[\u3040-\u309F]+$\",%s)"},
{L"^[\x20-\x7E]+$", L"regex_match(\"^[\x20-\x7E]+$\",%s)"},
{L"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$", L"regex_match(E-Mail,%s)"}
};
std::function<std::wstring(UnicodeString)> str_towstr{[](UnicodeString linp)
{
std::wstring out;
for (int i=0; i < linp.Length(); i++) {
#ifndef _WIN64
out +=linp[i];
#else
out +=linp[i+1];
#endif
}
return out;
}
};
std::wregex wregex1(std::get<0>(v1[ComboBox1->ItemIndex]).c_str());
std::wstring in_str = str_towstr(Edit1->Text);
UnicodeString l_value = Format(std::get<1>(v1[ComboBox1->ItemIndex]).c_str(), ARRAYOFCONST((Edit1->Text)) );
if (std::regex_match(in_str, wregex1))
{
Memo1->Lines->Append("Successful " + l_value);
Edit1->Text = L"";
}
else
{
Memo1->Lines->Append("NO " + l_value);
}
}
ComboBox1中のパターンを選択し、Edit1に文字列を入れてSpeedButton1をタップします。
パターンマッチしている場合はSuccessfulがMemo1に表示されます。