[Solved] A Search RegEx Problem

Discuss the word processor
Post Reply
Butch1
Posts: 31
Joined: Wed Apr 05, 2017 12:52 pm

[Solved] A Search RegEx Problem

Post by Butch1 »

Hi!

I would like to search for (and replace) substrings like this:
a.*b
i.e. beginning with a, then any number of other characters, then THE FIRST occuring b.
Example:
a12345b6789b1234 > a12345b (not a12345b6789b).

Thank you in advance!
Last edited by Hagar Delest on Mon Apr 17, 2017 10:33 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 7.4.5.1 (Portable)
User avatar
RoryOF
Moderator
Posts: 34616
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: A Search RegEx Problem

Post by RoryOF »

Do you wish to discard all characters after the first "b"?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Butch1
Posts: 31
Joined: Wed Apr 05, 2017 12:52 pm

Re: A Search RegEx Problem

Post by Butch1 »

??? No! As described: I want to search for strings a.....b and replace them.
LibreOffice 7.4.5.1 (Portable)
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: A Search RegEx Problem

Post by keme »

Regular expressions use "greedy" matching, so they will match the largest chunk they can.

You want a RegEx to "find 'a', then the first 'b' after a number of arbitrary characters". To put that into a RegEx is simplest when you partially invert the logic: "find a 'b' after a number of characters which are not 'b'."

In regex that would be

Code: Select all

a[^b]*b
Apache OO 4.1.12 and LibreOffice 7.5, mostly on Ms Windows 10
Butch1
Posts: 31
Joined: Wed Apr 05, 2017 12:52 pm

Re: A Search RegEx Problem

Post by Butch1 »

@keme:
Thank you very much!!!
Unfortunately my question was a reduced one: My real problem is to find the smallest chunk beginning with a string and ending with another string. Something like this:
abcStringAdefghStringBijklmnStringBopqrts
Result: StringAdefghStringB
Do you have a hint for this case too?
Butch
LibreOffice 7.4.5.1 (Portable)
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: A Search RegEx Problem

Post by karolus »

Hallo

you need an non-greedy expression:

stringa.*?stringb
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Butch1
Posts: 31
Joined: Wed Apr 05, 2017 12:52 pm

Re: A Search RegEx Problem

Post by Butch1 »

@karolus:
Hallelujah!
Thank you!!!
LibreOffice 7.4.5.1 (Portable)
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: A Search RegEx Problem

Post by keme »

Great! I learned something. Thanks, Karolus.
Apache OO 4.1.12 and LibreOffice 7.5, mostly on Ms Windows 10
Post Reply