1、TRegExOption = (roNone, /无roIgnoreCase, /不区分大小写roMultiLine, /多行模式; 可使 和 $ 匹配每个行首或行尾roExplicitCapture, /只捕获指定了名称或编号的子表达式roCompiled, /预编译表达式; 这在反复使用更有效率roSingleLine, /单行模式; 使 . 也可匹配换行符roIgnorePatternSpace /忽略注释和未经转义的空白);uses RegularExpressions;roIgnoreCaseprocedure TForm1.Button1Click(Sender: TObject)
2、;constpattern = A-Z+d+;txt = AAA1 bbb2 aa11 bb22 A111 B222 AAAA;varmatch: TMatch;beginMemo1.Clear;for match in TRegEx.Matches(txt, pattern, roIgnoreCase) dobeginMemo1.Lines.Add(match.Value);end;Memo1.Lines.Add(-);for match in TRegEx.Matches(txt, pattern) dobeginMemo1.Lines.Add(match.Value);end;end;*
3、AAA1bbb2aa11bb22A111B222-AAA1A111B222*roMultiLineprocedure TForm1.Button2Click(Sender: TObject);consttxt = Delphi Delphi Delphi#13#10Delphi Delphi Delphi;varrStr: string;beginMemo1.Clear;行首rStr := TRegEx.Replace(txt, Delphi, , roMultiLine);Memo1.Lines.Add(rStr);Memo1.Lines.Add(-);rStr := TRegEx.Repl
4、ace(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 DelphiDelphi Delphi-Delphi DelphiDelphi Delphi Delphi-Delphi Delphi Delp
5、hi Delphi -Delphi Delphi DelphiDelphi Delphi *roExplicitCaptureprocedure TForm1.Button3Click(Sender: TObject);constpattern1 = (A-Z+)(d+);pattern2 = (?A-Z+)(d+);pattern3 = (A-Z+)(?d+);txt = AA11 BB22;varmatch: TMatch;group: TGroup;beginMemo1.Clear;for match in TRegEx.Matches(txt, pattern1, roExplicit
6、Capture) do beginfor group in match.Groups dobeginMemo1.Lines.Add(group.Value);end;end;Memo1.Lines.Add(-);for match in TRegEx.Matches(txt, pattern1) do /此处把 pattern1 换做 pattern2 或 pattern3 均可beginfor group in match.Groups dobeginMemo1.Lines.Add(group.Value);end;end;Memo1.Lines.Add(-);for match in TR
7、egEx.Matches(txt, pattern2, roExplicitCapture) do beginfor group in match.Groups dobeginMemo1.Lines.Add(group.Value);end;end;Memo1.Lines.Add(-);for match in TRegEx.Matches(txt, pattern3, roExplicitCapture) do beginfor group in match.Groups dobeginMemo1.Lines.Add(group.Value);end;end;end;*AA11BB22-AA
8、11AA11BB22BB22-AA11AABB22BB-AA1111BB2222*roCompiledprocedure TForm1.Button4Click(Sender: TObject);varreg: TRegEx;beginreg := TRegEx.Create(d+, roCompiled);Memo1.Text := reg.Replace(AA11 BB22, ); /AA BBend;roSingleLineprocedure TForm1.Button5Click(Sender: TObject);const pattern = A-Z1.1;txt = A B C D
9、#13#10A B C D#13#10A B C D;varrStr: string;beginMemo1.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;*1111111111111111111111D-111111D111111D111111D*roIgnorePatternSpaceprocedure
10、TForm1.Button6Click(Sender: TObject);varrStr: string;beginMemo1.Clear;忽略空白 rStr := TRegEx.Replace(ABC,A B C,AB C, AB C, ., roIgnorePatternSpace);Memo1.Lines.Add(rStr); /.,A B C,AB CrStr := TRegEx.Replace(ABC,A B C,AB C, AB C, .);Memo1.Lines.Add(rStr); /ABC,A B C,.Memo1.Lines.Add(-);使用注释rStr := TRegE
11、x.Replace(ABC123, ABC#*123, ., roIgnorePatternSpace);Memo1.Lines.Add(rStr); /.123rStr := TRegEx.Replace(ABC123, ABC#*123, .);Memo1.Lines.Add(rStr); /.end;*.,A B C,AB CABC,A B C,.-.123.*选项集合procedure TForm1.Button7Click(Sender: TObject);constpattern = Delphi;txt = DELPHI DELPHI DELPHI#13#10delphi delphi delphi;beginMemo1.Text := TRegEx.Replace(txt, pattern, , roIgnoreCase, roMultiLine);end;*DELPHI DELPHIdelphi delphi*