程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 官方的正則表達式組件 RegularExpressions (4) : 表達式選項

官方的正則表達式組件 RegularExpressions (4) : 表達式選項

編輯:Delphi

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; 
{********************* 
...... DELPHI Delphi 
...... delphi Delphi 
**********************} 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved