澳门威利斯人_威利斯人娱乐「手机版」

来自 威利斯人娱乐 2019-08-03 10:37 的文章
当前位置: 澳门威利斯人 > 威利斯人娱乐 > 正文

官方的正则表达式组件

  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  **********************}  


本文由澳门威利斯人发布于威利斯人娱乐,转载请注明出处:官方的正则表达式组件

关键词: 澳门威利斯人