Delphi XE 中使用正则表达式

2018-10-31

uses RegularExpressions; //相关单元 
 
const 
  pattern = ’[A-Z]+d+’; //测试用的表达式 
  txt = ’AAA1 BBB2 AA11 BB22 A111 B222 AAAA’; //测试用的目标文本 
 
{是否匹配成功} 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  if TRegEx.IsMatch(txt, pattern) then 
  begin 
    ShowMessage(’有匹配到’); 
  end; 
end; 
 
{获取第一个匹配结果} 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  match: TMatch; 
begin 
  match := TRegEx.Match(txt, pattern); 
  if match.Success then //或用一句话 if TRegEx.Match(txt, pattern).Success then 
  begin 
    ShowMessage(match.Value); //AAA1 
  end; 
end; 
 
{获取所有匹配结果} 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  matchs: TMatchCollection; 
  match: TMatch; 
  i: Integer; 
begin 
  matchs := TRegEx.Matches(txt, pattern); 
 
  Memo1.Clear; 
  for match in matchs do 
  begin 
    Memo1.Lines.Add(match.Value); 
  end; 
 
  Memo1.Lines.Add(’----------’); 
  for i := 0 to matchs.Count - 1 do 
  begin 
    Memo1.Lines.Add(matchs[i].Value); 
  end; 
end; 
 
{使用 TMatch 对象的 NextMatch 遍历匹配结果时,需实例化对象} 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  reg: TRegEx; 
  match: TMatch; 
begin 
  reg := TRegEx.Create(pattern); 
  match := reg.Match(txt); 
 
  Memo1.Clear; 
  while match.Success do 
  begin 
    Memo1.Lines.Add(match.Value); 
    match := match.NextMatch; 
  end; 
end; 
 
{替换} 
procedure TForm1.Button6Click(Sender: TObject); 
begin 
  Memo1.Text := TRegEx.Replace(txt, pattern, ’xxx’); //xxx xxx xxx xxx xxx xxx AAAA 
end; 
 
{分割} 
procedure TForm1.Button7Click(Sender: TObject); 
var 
  rArr: TArray<string>; 
  s: string; 
begin 
  rArr := TRegEx.Split(’AAA,BBB;CCC,DDD EEE’, ’[,; ]’); 
 
  Memo1.Clear; 
  for s in rArr do 
  begin 
    Memo1.Lines.Add(s); //AAA/BBB/CCC/DDD/EEE 
  end; 
end; 
 
{TRegEx 还有一个 class 方法 Escape, 用于给特殊字符转义} 
procedure TForm1.Button8Click(Sender: TObject); 
begin 
  Memo1.Text := TRegEx.Escape(’[]^$.|?*+(){}’); //: []^$.|?*+(){} 
end; 

uses RegularExpressions; 
 
const 
  pattern = ’([A-Z]+)(d+)’; //其三个子表达式分别是:[A-Z]+d+  [A-Z]+  d+ 
  txt = ’AAA1 BBB2 AA11 BB22 A111 B222 AAAA’; //测试用的目标文本 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  match: TMatch; 
  group: TGroup; 
  groups: TGroupCollection; 
begin 
  match := TRegEx.Match(txt, pattern); 
  groups := match.Groups; 
 
  Memo1.Clear; 
  for group in groups do 
  begin 
    Memo1.Lines.Add(group.Value); //AAA1/AAA/1 
  end; 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  match: TMatch; 
  group: TGroup; 
begin 
  Memo1.Clear; 
  for match in TRegEx.Matches(txt, pattern) do 
  begin 
    for group in match.Groups do 
    begin 
      Memo1.Lines.Add(group.Value); 
    end; 
    Memo1.Lines.Add(’’); 
  end; 
end; 
{************** 
AA11 
AA 
11 
 
BB22 
BB 
22 
 
A111 
A 
111 
 
B222 
B 
222 
**************} 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  rStr: string; 
begin 
  Memo1.Clear; 
  rStr := TRegEx.Replace(txt, pattern, ’$1’);  //AAA BBB AA BB A B AAAA 
  Memo1.Lines.Add(rStr); 
 
  rStr := TRegEx.Replace(txt, pattern, ’$2’);  //1 2 11 22 111 222 AAAA 
  Memo1.Lines.Add(rStr); 
 
  rStr := TRegEx.Replace(txt, pattern, ’$2$1’); //1AAA 2BBB 11AA 22BB 111A 222B AAAA 
  Memo1.Lines.Add(rStr); 
end; 
{********************************** 
AAA BBB AA BB A B AAAA 
1 2 11 22 111 222 AAAA 
1AAA 2BBB 11AA 22BB 111A 222B AAAA 
***********************************} 


unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, RegularExpressions; 
 
type 
  TForm1 = class(TForm) 
    Memo1: TMemo; 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
  private 
    function MyMatchEvaluator(const Match: TMatch): string; //TMatchEvaluator = function(const Match: TMatch): string of object; 
  public 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
const 
  pattern = ’[A-Z]+d+’; 
  txt = ’AAA1 BBB2 AA11 BB22 A111 B222 AAAA’; 
 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  Memo1.Text := TRegEx.Replace(txt, pattern, MyMatchEvaluator); //aaa1 bbb2 aa11 bb22 a111 b222 AAAA 
end; 
 
function TForm1.MyMatchEvaluator(const Match: TMatch): string; 
begin 
  Result := LowerCase(Match.Value); 
end; 
 
end. 


TRegExOption = ( 
  roNone,              //无 
  roIgnoreCase,        //不区分大小写 
  roMultiLine,        //多行模式; 可使 ^ 和 $ 匹配每个行首或行尾 
  roExplicitCapture,  //只捕获指定了名称或编号的子表达式 
  roCompiled,          //预编译表达式; 这在反复使用更有效率 
  roSingleLine,        //单行模式; 使 . 也可匹配换行符 
  roIgnorePatternSpace //忽略注释和未经转义的空白 
); 

uses RegularExpressions; 
 
{roIgnoreCase} 
procedure TForm1.Button1Click(Sender: TObject); 
const 
  pattern = ’[A-Z]+d+’; 
  txt = ’AAA1 bbb2 aa11 bb22 A111 B222 AAAA’; 
var 
  match: TMatch; 
begin 
  Memo1.Clear; 
  for match in TRegEx.Matches(txt, pattern, [roIgnoreCase]) do 
  begin 
    Memo1.Lines.Add(match.Value); 
  end; 
 
  Memo1.Lines.Add(’----------’); 
 
  for match in TRegEx.Matches(txt, pattern) do 
  begin 
    Memo1.Lines.Add(match.Value); 
  end; 
end; 
{********************* 
AAA1 
bbb2 
aa11 
bb22 
A111 
B222 
---------- 
AAA1 
A111 
B222 
**********************} 
 
{roMultiLine} 
procedure TForm1.Button2Click(Sender: TObject); 
const 
  txt = ’Delphi Delphi Delphi’#13#10’Delphi Delphi Delphi’; 
var 
  rStr: string; 
begin 
  Memo1.Clear; 
  {行首} 
  rStr := TRegEx.Replace(txt, ’^Delphi’, ’......’, [roMultiLine]); 
  Memo1.Lines.Add(rStr); 
  Memo1.Lines.Add(’--------------------’); 
  rStr := TRegEx.Replace(txt, ’^Delphi’, ’......’); 
  Memo1.Lines.Add(rStr); 
 
  Memo1.Lines.Add(’--------------------’); 
  {行尾} 
  rStr := TRegEx.Replace(txt, ’Delphi$’, ’......’, [roMultiLine]); 
  Memo1.Lines.Add(rStr); 
  Memo1.Lines.Add(’--------------------’); 
  rStr := TRegEx.Replace(txt, ’Delphi$’, ’......’); 
  Memo1.Lines.Add(rStr); 
end; 
{********************* 
...... Delphi Delphi 
...... Delphi Delphi 
-------------------- 
...... Delphi Delphi 
Delphi Delphi Delphi 
-------------------- 
Delphi Delphi ...... 
Delphi Delphi ...... 
-------------------- 
Delphi Delphi Delphi 
Delphi Delphi ...... 
**********************} 
 
{roExplicitCapture} 
procedure TForm1.Button3Click(Sender: TObject); 
const 
  pattern1 = ’([A-Z]+)(d+)’; 
  pattern2 = ’(?<name1>[A-Z]+)(d+)’; 
  pattern3 = ’([A-Z]+)(?<3>d+)’; 
  txt = ’AA11 BB22’; 
var 
  match: TMatch; 
  group: TGroup; 
begin 
  Memo1.Clear; 
 
  for match in TRegEx.Matches(txt, pattern1, [roExplicitCapture]) do 
  begin 
    for group in match.Groups do 
    begin 
      Memo1.Lines.Add(group.Value); 
    end; 
  end; 
  Memo1.Lines.Add(’--------------------’); 
 
  for match in TRegEx.Matches(txt, pattern1) do //此处把 pattern1 换做 pattern2 或 pattern3 均可 
  begin 
    for group in match.Groups do 
    begin 
      Memo1.Lines.Add(group.Value); 
    end; 
  end; 
  Memo1.Lines.Add(’--------------------’); 
  
  for match in TRegEx.Matches(txt, pattern2, [roExplicitCapture]) do 
  begin 
    for group in match.Groups do 
    begin 
      Memo1.Lines.Add(group.Value); 
    end; 
  end; 
  Memo1.Lines.Add(’--------------------’); 
  
  for match in TRegEx.Matches(txt, pattern3, [roExplicitCapture]) do 
  begin 
    for group in match.Groups do 
    begin 
      Memo1.Lines.Add(group.Value); 
    end; 
  end; 
end; 
{********************* 
AA11 
BB22 
-------------------- 
AA11 
AA 
11 
BB22 
BB 
22 
-------------------- 
AA11 
AA 
BB22 
BB 
-------------------- 
AA11 
11 
BB22 
22 
**********************} 
 
{roCompiled} 
procedure TForm1.Button4Click(Sender: TObject); 
var 
  reg: TRegEx; 
begin 
  reg := TRegEx.Create(’d+’, [roCompiled]); 
  Memo1.Text := reg.Replace(’AA11 BB22’, ’..’);  //AA.. BB.. 
end; 
 
{roSingleLine} 
procedure TForm1.Button5Click(Sender: TObject); 
const           
  pattern = ’[A-Z]{1}.{1}’; 
  txt = ’A B C D’#13#10’A B C D’#13#10’A B C D’; 
var 
  rStr: string; 
begin 
  Memo1.Clear; 
  rStr := TRegEx.Replace(txt, pattern, ’11’, [roSingleLine]); 
  Memo1.Lines.Add(rStr); 
  Memo1.Lines.Add(’--------------------’); 
  
  rStr := TRegEx.Replace(txt, pattern, ’11’); 
  Memo1.Lines.Add(rStr); 
end; 
{********************* 
11111111 
11111111 
111111D 
-------------------- 
111111D 
111111D 
111111D 
**********************} 
 
{roIgnorePatternSpace} 
procedure TForm1.Button6Click(Sender: TObject); 
var 
  rStr: string; 
begin 
  Memo1.Clear; 
  {忽略空白}                                
  rStr := TRegEx.Replace(’ABC,A B C,AB C’, ’AB C’, ’...’, [roIgnorePatternSpace]); 
  Memo1.Lines.Add(rStr); //...,A B C,AB C 
  rStr := TRegEx.Replace(’ABC,A B C,AB C’, ’AB C’, ’...’); 
  Memo1.Lines.Add(rStr); //ABC,A B C,... 
  Memo1.Lines.Add(’--------------------’); 
 
  {使用注释} 
  rStr := TRegEx.Replace(’ABC123’, ’ABC#*123’, ’...’, [roIgnorePatternSpace]); 
  Memo1.Lines.Add(rStr); //...123 
  rStr := TRegEx.Replace(’ABC123’, ’ABC#*123’, ’...’); 
  Memo1.Lines.Add(rStr); //... 
end; 
{********************* 
...,A B C,AB C 
ABC,A B C,... 
-------------------- 
...123 
... 
**********************} 
 
{选项集合} 
procedure TForm1.Button7Click(Sender: TObject); 
const 
  pattern = ’^Delphi’; 
  txt = ’DELPHI DELPHI DELPHI’#13#10’delphi delphi delphi’; 
begin 
  Memo1.Text := TRegEx.Replace(txt, pattern, ’......’, [roIgnoreCase, roMultiLine]); 
end; 


阅读151