[fpc-pascal] A Regular Expression failing on the borders of words

luciano de souza luchyanus at gmail.com
Thu Apr 10 07:43:41 CEST 2014


Hello all,

This program compiles, but it gives a wrong output.

program ertext;
{$mode objfpc}{$H+}

uses
Sysutils, regexpr;

var
r: TRegexpr;
i: integer;

BEGIN
r := TRegexpr.create;
try
r.expression := '^(x\s+)*(\([A-E]\))*(\s*.*\.)+(\s+\+.*\b)*(\s+ at .*\b)*$';
if r.exec('x (A) Write a report. +ABC +DEF @John @Mary') then
begin
for i := 1 to r.SubexprMatchCount do
begin
writeln(r.match[i]);
readln;
end;
end;

finally
r.free;
end;
END.

The input string is "x (A) Write a report. +ABC +DEF @John @Mary".

The expected output was:

r.match[1] -> " x" -> OK
r.match[2] -> "(A)" -> OK
r.match[3] -> "Write a report." -> OK
r.match[4] -> " +ABC +DEF" -> Error
r.match[5] -> " @John @Mary" -> Error

In stead of 4 e 5, I got only 4 with the following output: " +ABC +DEF
@John @Mary".

It's true I am not have enough experience in Regular Expressions, but
the used one seems to be very logical for me. Would Someone have any
idea what is wrong?

Best regards,

-- 
Luciano de Souza


More information about the fpc-pascal mailing list