[Solved] Regular Expression matched value replacement

Discuss the spreadsheet application
Locked
Phillip
Posts: 110
Joined: Wed Jan 09, 2013 1:50 pm

[Solved] Regular Expression matched value replacement

Post by Phillip »

I want to re-arrange
=IF(ISNUMBER(E5); IF(NOW()>AgeT+E5;"#";"");"")
to be
=IF(ISNUMBER(E5); IF(NOW()>E5+AgeT;"#";"");"")

However Find & Replace - using Regular Expressions in More Options
Search for: >AgeT\+(.*);
Replace with: >$1+AgeT;
Results in: =IF(ISNUMBER(E5); IF(NOW()>E5;"#";"")+AgeT;"")

It would seem that the "(.*);" matches all characters after "AgeT+" until the last ";" instead of the first ";". Why?
NB.Search for: >AgeT\+([^;].*); does the same !
Last edited by MrProgrammer on Wed Jun 24, 2026 6:09 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved]
Apache Open Office 4.1.16, Windows 7 Professional 64-bit
Jan_J
Posts: 201
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Regular Expression stored value replacement

Post by Jan_J »

Regular expressions behave greedy, i.e. they try to find the widest acceptable phrase
You can go two ways:
(1) as you tried >AgeT\+([^;].*); but there you did not express what you wanted. Correct to >AgeT\+([^;]*);
or
(2) convert pattern to nongreedy using ? specifier: >AgeT\+(.*?);
// corrected the former misspell >AgeT\+(.*);?

Both should be equivalent.
Last edited by Jan_J on Tue Jun 09, 2026 10:51 pm, edited 1 time in total.
JJ ∙ https://forum.openoffice.org/pl/
LO (26.2) ∙ Python (3.14|3.10) ∙ Unicode 17 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Phillip
Posts: 110
Joined: Wed Jan 09, 2013 1:50 pm

Re: Regular Expression stored value replacement

Post by Phillip »

Thanks Jan-J - my error in adding the extra "."
When I tried: >AgeT\+([^;]*);
Works OK - Result: =IF(ISNUMBER(E5); IF(NOW()>E5+AgeT;"#";"");"")

NB Also tried: >AgeT\+(.*);?
Didn't work - Result: =IF(ISNUMBER(E5); IF(NOW()>E5;"#";"");"")+AgeT;
Perhaps because you are on Unix and I am on Windows?
Apache Open Office 4.1.16, Windows 7 Professional 64-bit
Jan_J
Posts: 201
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Regular Expression stored value replacement

Post by Jan_J »

Phillip wrote:my error in adding the extra "."
Indeed. [^;].* means `any characters in a sequence of arbitrary length, preceded by single non-semicolon`.
[^;]* means `any sequence of non-semicolon characters`.
[^;]+ is the same, except that the phrase must have nonzero length.
Phillip wrote:Also tried: >AgeT\+(.*);?
Well, my fault in hurry. Should be >AgeT\+(.*?);

U**X, Windows: no matter. The regex library is the same in OpenOffice.
JJ ∙ https://forum.openoffice.org/pl/
LO (26.2) ∙ Python (3.14|3.10) ∙ Unicode 17 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Phillip
Posts: 110
Joined: Wed Jan 09, 2013 1:50 pm

Re: Regular Expression stored value replacement

Post by Phillip »

Many thanks. Tried: >AgeT\+(.*?); worked ok. I could not find an explanation for the "?", will look again.
I use regex a lot with SED on windows - one difference is Windows requires a " instead of ' as in sed -i -r "s/^[0-9]+.*-/,-/g" file.txt
Apologies for "U**X" just saw the reference to "Unix tools".
Apache Open Office 4.1.16, Windows 7 Professional 64-bit
User avatar
robleyd
Moderator
Posts: 5528
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Regular Expression stored value replacement

Post by robleyd »

I could not find an explanation for the "?"
From the HELP:
?   Finds zero or one of the characters in front of the "?". For example, "Texts?"
    finds "Text" and "Texts" and "x(ab|c)?y" finds "xy", "xaby", or "xcy".
Slackware 15 (current) 64 bit
Apache OpenOffice.1.16
LibreOffice 26.8.0.3; SlackBuild for 26.8.0 by Eric Hameleers
-----------
I hate this damn computer, I wish that I could sell it.
It won't do what I want it to, Only what I tell it.
Jan_J
Posts: 201
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Regular Expression stored value replacement

Post by Jan_J »

More precisely, or more subtle: ? also changes context of pattern matching from greedy (find the widest case) to conservative (find the narrowest one).
It is not explicitly stated in OO/LO help. However, there's a link from LO help (https://help.libreoffice.org/latest/en- ... 00001.html?)
For a full list of supported metacharacters and syntax, see ICU Regular Expressions documentation
to ICU Regex service (https://unicode-org.github.io/icu/userg ... characters), and there you read:
? Match zero or one times. Prefer one.
*? Match 0 or more times. Match as few times as possible.
+? Match 1 or more times. Match as few times as possible.
That's it.
JJ ∙ https://forum.openoffice.org/pl/
LO (26.2) ∙ Python (3.14|3.10) ∙ Unicode 17 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Locked