Delphi Regular Expression Syntax (6) greedy match and non-greedy match

Source: Internet
Author: User
Tags expression regular expression

Greedy match

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := '   reg.RegEx  := '<.*>'; //将会全部匹配, 因为两头分别是: < 和 >
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: ◆
  FreeAndNil(reg);
end;

Non-greedy match

var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);
  reg.Subject := '   reg.RegEx  := '<.*?>'; // *? 是非贪婪匹配方法之一, 将匹配每 组 <>
  reg.Replacement := '◆';
  reg.ReplaceAll;
  ShowMessage(reg.Subject); //返回: ◆◆◆标题◆◆◆内容◆◆
  FreeAndNil(reg);
end;

More greed and non-greedy match control:

Greedy match Non-greedy match Describe
? ?? 0 or 1
+ +? 1 or more
* *? 0 or more
N {n}? N A
{N,m} {n,m}? N-m A
{N,} {N,}? N or more

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.