加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

官方的正则表达式组件 RegularExpressions (4) : 表达式选项

发布时间:2020-12-14 04:37:25 所属栏目:百科 来源:网络整理
导读:TRegExOption = ( roNone,//无 roIgnoreCase,//不区分大小写 roMultiLine,//多行模式; 可使 ^ 和 $ 匹配每个行首或行尾 roExplicitCapture,//只捕获指定了名称或编号的子表达式 roCompiled,//预编译表达式; 这在反复使用更有效率 roSingleLine,//单行模式;

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,'......');
  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,'11',[roSingleLine]);
  Memo1.Lines.Add(rStr);
  Memo1.Lines.Add('--------------------');
  
  rStr := TRegEx.Replace(txt,'11');
  Memo1.Lines.Add(rStr);
end;
{*********************
22222111
22222111
222221D
--------------------
222221D
222221D
222221D
**********************}

{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); //...,AB C
  rStr := TRegEx.Replace('ABC,'...');
  Memo1.Lines.Add(rStr); //ABC,...
  Memo1.Lines.Add('--------------------');

  {使用注释}
  rStr := TRegEx.Replace('ABC123','ABC#*123',[roIgnorePatternSpace]);
  Memo1.Lines.Add(rStr); //...123
  rStr := TRegEx.Replace('ABC123','...');
  Memo1.Lines.Add(rStr); //...
end;
{*********************
...,AB C
ABC,...
--------------------
...123
...
**********************}

{选项集合}
procedure TForm1.Button7Click(Sender: TObject);
const
  pattern = '^Delphi';
  txt = 'DELPHI DELPHI DELPHI'#13#10'delphi delphi delphi';
begin
  Memo1.Text := TRegEx.Replace(txt,[roIgnoreCase,roMultiLine]);
end;
{*********************
...... DELPHI DELPHI
...... delphi delphi
**********************}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读