Recent

Author Topic: RegExpr matchting empty string  (Read 1727 times)

fred

  • Full Member
  • ***
  • Posts: 208
RegExpr matchting empty string
« on: April 09, 2026, 02:56:26 pm »
Hello,

When I try to match a empty string with a a regular expression like this:
Code: Pascal  [Select][+][-]
  1.   writeln(RegExpr.ExecRegExpr('^$', ''));
I get als result: false
I expected this to be true, what am I doing wrong?

Tried also the TRExExpr class with property EmptyInputRaisesError true of false but still got an error with a empty expression.

I must be getting old :)

Fred.
Lazarus 4.6/fpc 3.2.2

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12345
  • Debugger - SynEdit - and more
    • wiki
Re: RegExpr matchting empty string
« Reply #1 on: April 09, 2026, 03:32:51 pm »
It seems to work with 3.3.1.

Or get the upstream regex project.

Thaddy

  • Hero Member
  • *****
  • Posts: 19175
  • Glad to be alive.
Re: RegExpr matchting empty string
« Reply #2 on: April 09, 2026, 04:04:35 pm »
I get true with trunk:
Code: Pascal  [Select][+][-]
  1. // full program
  2. uses regexpr;
  3. begin
  4.   writeln(RegExpr.ExecRegExpr('^$', ''));// returns TRUE
  5. end.
Afaik, the upstream is compatible with 3.2.2, though.
objects are fine constructs. You can even initialize them with constructors.

fred

  • Full Member
  • ***
  • Posts: 208
Re: RegExpr matchting empty string
« Reply #3 on: April 09, 2026, 04:26:05 pm »
Thanks Martin and Thaddy.

I took a quick look at regexpr.pas from 3.2.2 and 3.3.1 but did not see much difference in OP_BOL/OP_EOL.

My complete test program was:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc} {$H+}
  2.  
  3. uses sysutils, strutils, RegExpr;
  4.  
  5. begin
  6.   writeln(RegExpr.ExecRegExpr(ParamStr(1), ParamStr(2)));
  7.   writeln(RegExpr.ExecRegExpr('^$', ''));
  8. end.
  9.  

Running with InstantFPC.
So much like the one from Thaddy.
Strange...

For now I have made a workaround, when I have more time I will try to take a deeper look (like always "when more time"...).

Fred.

Thausand

  • Hero Member
  • *****
  • Posts: 545
Re: RegExpr matchting empty string
« Reply #4 on: April 09, 2026, 04:45:37 pm »
pascal
Code: Pascal  [Select][+][-]
  1. {$mode objfpc} {$H+}
  2. uses sysutils, strutils, RegExpr;
  3. begin
  4.   writeln(RegExpr.ExecRegExpr('^$', ''));
  5. end.
  6.  

3.2.2
Code: Bash  [Select][+][-]
  1. $ fpc-332 -B test.pas
  2. Free Pascal Compiler version 3.2.2 [2024/10/24] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling test.pas
  6. Linking test
  7. 8 lines compiled, 0.1 sec
  8. $ ./test
  9. TRUE
  10.  

3.3.1
Code: Bash  [Select][+][-]
  1. $ fpc331 -B test.pas
  2. Free Pascal Compiler version 3.3.1 [2025/03/01] for x86_64
  3. Copyright (c) 1993-2025 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling test.pas
  6. Linking test
  7. 8 lines compiled, 0.2 sec, 795008 bytes code, 370912 bytes data
  8. $ ./test
  9. TRUE
  10.  
A docile goblin always follow HERMES.md

Thaddy

  • Hero Member
  • *****
  • Posts: 19175
  • Glad to be alive.
Re: RegExpr matchting empty string
« Reply #5 on: April 09, 2026, 04:47:09 pm »
@Fred

There is an error in your code that causes an exception:
If the params array does not have paramstr(1),paramstr(2) the program crashes.
To fix, use paramcount:
Code: Pascal  [Select][+][-]
  1. // full program
  2. uses RegExpr;
  3. begin
  4.   if paramcount >= 2 then  // check!!!
  5.   begin
  6.     writeln(RegExpr.ExecRegExpr(ParamStr(1), ParamStr(2)));
  7.     writeln(RegExpr.ExecRegExpr('^$', ''));
  8.   end;
  9. end.
Paramstr(0), the executable name, is not counted towards paramcount.
That may be all. With two or more "''"  (empty)  parameters this prints true, true, otherwise prints false where the parameters are filled or otherwise does nothing.

(The double quotes are just illustrating, use '' or "" (two quotes of the same type))

BTW: not a bug: maps to argc/argv.
(which is a ppointer to an array of PChar closed with a double zero.)
« Last Edit: April 09, 2026, 05:09:26 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

fred

  • Full Member
  • ***
  • Posts: 208
Re: RegExpr matchting empty string
« Reply #6 on: April 10, 2026, 12:43:16 pm »
Thanks Thaddy.

I know, it was just a small test program.
Since I did not get it to work in a GUI program I made is back to the smallest program I could test.
I still make and like to use small command line tools since FLEX/6809, UniFLEX, OS-9/68k, want to write the line editor I used then :)

I used the regex for searching in a grid, with testing I could not find an empty cell with '^$'.
For know I use:
Code: Pascal  [Select][+][-]
  1.         if ((zoektekst = '^$') and (Cells[c, r] = ''))
  2.         or RegexObj.Exec(Cells[c, r])
  3.  
Not how I like it but it works.
When I have more time (...) I would test it using the debugger or install another version of the compiler.
An empty search string is not possible using FindDialog so that doesn't happen.
Or just make a search function for empty cells, would be a little faster.
It's for myself so not a problem.

Thanks all.

Edit:
Checked it on https://onecompiler.com/pascal/44jxcj6jz
and the result is TRUE
Interesting :)

Edit2:
Running in https://webflow.replit.com/language/online-pascal-compiler
and the result is FALSE
Very Interesting :)

Fred.


« Last Edit: April 10, 2026, 02:16:20 pm by fred »

Thaddy

  • Hero Member
  • *****
  • Posts: 19175
  • Glad to be alive.
Re: RegExpr matchting empty string
« Reply #7 on: April 10, 2026, 04:13:01 pm »
@Fred

For the online compilers, try
Code: Pascal  [Select][+][-]
  1. program whoami;
  2. // full program
  3. var
  4.   id:array[0..1023] of char;external name '__fpc_ident';
  5. begin
  6.   writeln(id);
  7. end.
The first link crashes and shows fpc 3.0.4 in its dump:
Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.0.4 [2017/10/03] for x86_64
  2. Copyright (c) 1993-2017 by Florian Klaempfl and others
  3. Target OS: Linux for x86-64
  4. Compiling main.pas
  5. Linking main
  6. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  7. /usr/bin/ld: main.o: in function `main':
  8. main.pas:(.text.n_main+0x1c): undefined reference to `__fpc_ident'
  9. main.pas(7,1) Error: Error while linking
  10. main.pas(7,1) Fatal: There were 1 errors compiling module, stopping
  11. Fatal: Compilation aborted
  12. Error: /usr/local/fpc-3.0.4/bin/ppcx64 returned an error exitcode


The second link passes and shows "FPC 3.2.2 [2024/05/14] for x86_64 - Linux"

I would trust the second link......

(The ID works from 3.2.0, but undocumented!!!)
« Last Edit: April 10, 2026, 04:26:10 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Thaddy

  • Hero Member
  • *****
  • Posts: 19175
  • Glad to be alive.
Re: RegExpr matchting empty string
« Reply #8 on: April 10, 2026, 04:35:27 pm »
Official, documented and serverside non-crashing version:
Code: Pascal  [Select][+][-]
  1. program whoami;
  2. {$macro on}
  3. begin
  4.   writeln(fpc_fullversion);
  5.   writeln(fpc_version,fpc_release,fpc_patch);
  6. end.
But that gives a bit less info.
« Last Edit: April 10, 2026, 04:44:59 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018