Jump to content
Jacek Laskowski

RegExpressions and preUnGreedy

Recommended Posts

I need to enable preUnGreedy option in TRegEx, PCRE supports this parameter:

 

unit System.RegularExpressionsCore;

interface
[...]

type
  TPerlRegExOptions = set of (
    preCaseLess,       // /i -> Case insensitive
    preMultiLine,      // /m -> ^ and $ also match before/after a newline, not just at the beginning and the end of the string
    preSingleLine,     // /s -> Dot matches any character, including \n (newline). Otherwise, it matches anything except \n
    preExtended,       // /x -> Allow regex to contain extra whitespace, newlines and Perl-style comments, all of which will be filtered out
    preAnchored,       // /A -> Successful match can only occur at the start of the subject or right after the previous match
    preUnGreedy,       // Repeat operators (+, *, ?) are not greedy by default (i.e. they try to match the minimum number of characters instead of the maximum)
    preNoAutoCapture   // (group) is a non-capturing group; only named groups capture
  );

But the PCRE parameters are set by mapping:

constructor TRegEx.Create(const Pattern: string; Options: TRegExOptions);
begin
  FOptions := Options;
  FRegEx := TPerlRegEx.Create;
  FRegEx.Options := RegExOptionsToPCREOptions(FOptions);   <--- mapping PCRE params to Delphi TRegEx
  if (roNotEmpty in Options) then
    FRegEx.State := [preNotEmpty];
  FRegEx.RegEx := Pattern;
  FNotifier := TScopeExitNotifier.Create(FRegEx);
  if (roCompiled in FOptions) then
    FRegEx.Compile;
end;

And for a reason unknown to me, this parameter was omitted:
 

function RegExOptionsToPCREOptions(Value: TRegExOptions): TPerlRegExOptions;
begin
  Result := [];
  if (roIgnoreCase in Value) then
    Include(Result, preCaseLess);
  if (roMultiLine in Value) then
    Include(Result, preMultiLine);
  if (roExplicitCapture in Value) then
    Include(Result, preNoAutoCapture);
  if roSingleLine in Value then
    Include(Result, preSingleLine);
  if (roIgnorePatternSpace in Value) then
    Include(Result, preExtended);
end;

Bug?

 

Is there any way to solve this?

 

Delphi 10.3

 

Edited by Jacek Laskowski

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×