Delphi is the expression of the syntax (2) or with the repetition

Source: Internet
Author: User
Tags expression repetition

// | The use of the number, | Yes, or it means.

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'CodeGear Delphi 2007';
  reg.RegEx  := 'e|Delphi|0'; //使用了 | 记号
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: Cod◆G◆ar ◆ 2◆◆7
  FreeAndNil(reg);
end;

+ is used, + is repeated 1 or more

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'ab+'; //使用了 + 记号, 这里是允许 a 后面有 1 个或 多个 b
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: a aa aaa aaaa ◆ ◆ ◆a a呀a
  FreeAndNil(reg);
end;

* The use of * is to repeat 0 or more

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'ab*'; //使用了 * 记号, 这里是允许 a 后面有多个或者 没有 b
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: ◆ ◆◆ ◆◆◆ ◆◆◆◆ ◆ ◆ ◆ ◆ ◆呀◆
  FreeAndNil(reg);
end;

// ? The use of,? is to repeat 0 or 1

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'a?'; //使用了 ? 记号
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: ◆ ◆◆ ◆◆◆ ◆◆◆◆ ◆b ◆bb ◆bbb◆ ◆呀◆
  FreeAndNil(reg);
end;

The use of curly braces <1>, specifying the number of repetitions

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'a{3}'; //这里指定重复 3 次
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: a aa ◆ ◆a ab abb abbba a呀 a
  FreeAndNil(reg);
end;

Use of curly braces <2>

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'a{2,4}'; //这里指定重复 2-4 次
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: a ◆ ◆ ◆ ab abb abbba a呀a
  FreeAndNil(reg);
end;

Use of curly braces <3>

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := 'a aa aaa aaaa ab abb abbba a呀a';
  reg.RegEx  := 'a{1,}'; //n 个或多个, 这里是 1 个或多个
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: ◆ ◆ ◆ ◆ ◆b ◆bb ◆bbb◆ ◆ 呀◆
  FreeAndNil(reg);
end;

The above {1,} and + are equivalent;

and {0,1} with? is equivalent;

{0,} and * are equivalent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.