Delphi XEのTRegExで正規表現を使用する方法を調べました。
RegularExpressions.TRegExレコード
TRegExは、オープンソースのPCREライブラリのラッパーです。
RegularExpressionsユニット(Delphi XE2以降はSystem.RegularExpressionsユニット)をusesして使用します。
uses RegularExpressions;
TRegExはレコード型なので、Freeは必要ありません。
IsMatchメソッド 入力文字列に一致が存在するか
クラスメソッドを使う例
if TRegEx.IsMatch('Delphi', '[A-Z]+') then
ShowMessage('Match');
インスタンスメソッドを使う例
var
regex: TRegEx;
begin
regex := TRegEx.Create('[A-Z]+');
if regex.IsMatch('Delphi') then
ShowMessage('Match');
Matcheメソッド 正規表現に一致する最初の文字列を取得する
MatcheメソッドはTMatchレコードを返します。
クラスメソッドを使う例
var
match: TMatch;
begin
match := TRegEx.Match('Delphi XE', '[A-Z]+');
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> D (1, 1)
インスタンスメソッドを使う例
var
regex: TRegEx;
match: TMatch;
begin
regex := TRegEx.Create('[A-Z]+');
match := regex.Match('Delphi XE');
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> D (1, 1)
TMatchのNextMatchメソッドで次の一致した文字列を取得できます。
var
regex: TRegEx;
match: TMatch;
begin
regex := TRegEx.Create('[A-Z]+');
match := regex.Match('Delphi XE');
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> D (1, 1)
//次の一致
match := match.NextMatch;
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> XE (8, 2)
TMatchのSuccessプロパティで一致したかどうかを調べることができます。
var
regex: TRegEx;
match: TMatch;
begin
regex := TRegEx.Create('[A-Z]+');
match := regex.Match('Delphi XE');
while match.Success do
begin
ShowMessage(match.Value);
match := match.NextMatch();
end;
グループ化した正規表現に一致した文字列は、TMatchのGroupsプロパティで取得できます。
var
match: TMatch;
group: TGroup;
begin
match := TRegEx.Match('2012/5/14', '(\d+)/(\d+)/(\d)');
ShowMessage(IntToStr(match.Groups.Count)); //=> 4
for group in match.Groups do
begin
ShowMessage(Format('%s (%d, %d)',
[group.Value, group.Index, group.Length]));
end;
(* 出力結果
2012/5/1 (1, 8)
2012 (1, 4)
5 (6, 1)
1 (8, 1)
*)
Matchesメソッド 正規表現に一致するすべての文字列を取得する
Matchesメソッドは、TMatchCollectionレコードを返します。
var
match: TMatch;
matches: TMatchCollection;
begin
matches := TRegEx.Matches('Delphi XE', '[A-Z]+');
ShowMessage(IntToStr(matches.Count));
match := matches.Item[0];
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> D (1, 1)
match := matches.Item[1];
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length])); //=> XE (8, 2)
Matchesメソッドは、for in doに対応しています。
var
regex: TRegEx;
match: TMatch;
matches: TMatchCollection;
begin
regex := TRegEx.Create('[A-Z]+');
matches := regex.Matches('Delphi XE');
ShowMessage(IntToStr(matches.Count)); //一致した数
for match in matches do
ShowMessage(
Format('%s (%d, %d)',
[match.Value, match.Index, match.Length]));
Replaceメソッド 正規表現に一致した文字列を置き換える
//アルファベットの大文字を_に置き換える
s := TRegEx.Replace('Delphi XE', '[A-Z]', '_');
ShowMessage(s); //=> _elphi __
引数に関数を渡すことで、マッチした文字にあわせて置換する文字を指定できます。
(* マッチした数値に1を加える *)
function TForm1.IncNum(const Match: TMatch): string;
begin
Result := IntToStr(StrToInt(Match.Value) + 1);
end;
//正規表現に一致した文字を関数処理して置き換える
var
s: UnicodeString;
begin
s := TRegEx.Replace('Delphi 2009', '\d+', IncNum);
ShowMessage(s); //=> Delphi 2010
Splitメソッド 文字列を分割する
var
splits: TArray<string>;
s: string;
begin
//半角スペースかスラッシュで分割する
splits := TRegEx.Split('Delphi C++Builder/RadPHP', '[ /]');
for s in splits do
Memo1.Lines.Add(s);
Escapeメソッド 特殊文字をエスケープする
var
s: string;
begin
s := TRegEx.Escape('C++');
ShowMessage(s); //=> C\+\+
正規表現の文法を検証する
コンストラクタの引数にroCompiledを指定すると、正規表現をコンパイルします。
正規表現に問題があれば例外を投げます。
try
TRegEx.Create(Edit1.Text, [roCompiled]);
except on E: Exception do
ShowMessage(E.Message);
end;
Pingback: Delphi XEでSkRegExpを使って正規表現を使用する « 山本隆の開発日誌