Find/Replace Regex

Hi.
I am trying to find and replace some text with regex in calc. Can anyone help with how I make it non-greedy.
I tried \([:digit:].*?[:digit:]\) in place of \([:digit:].*[:digit:]\) but both are greedy. When the following (12-34-23) cat (233) is in a selected cell it replaces both instances between ().

Thanks, steve

\([:digit:].*?[:digit:]\)

works as non-greedy for me (two replacements), and just ".*" as greedy (1 repl.) for your example.

I am trying to find and replace some text with regex in calc. Can anyone help with how I make it non-greedy.

I think the answer is that you don't: you must find a way of constructing a suitable expression despite such expressions being greedy.

I tried \([:digit:].*?[:digit:]\) in place of \([:digit:].*[:digit:]\) but both are greedy. When the following (12-34-23) cat (233) is in a selected cell it replaces both instances between ().

By the way, I don't understand your ".*?" - do you perhaps mean either ".*" or ".?" ?

I'm afraid you've fallen into the common trap of attempting to define your problem by quoting only expressions that *don't* work! Which parenthesised string do you want to match? I'm guessing the first. But what exactly are the characteristics of the strings you want to match? And the ones you want not to match?

Is the target string always at the beginning? Try:
\<\([^\)]*\)

Is the target string always digits-hyphen-digits-hyphen-digits? Try:
\([:digit:]+-[:digit:]+-[:digit:]+\)
or:
\(\d+-\d+-\d+\)

Do you want to match the second string instead? Try:
\([:digit:]+\)
or:
\(\d+\)

Depending on exactly what you want to match and to avoid matching, you may have to do something different.

I trust this helps.

Brian Barker

Thanks for the replies.
  Sorry I am not that clear now I read it again. In non-LO regex I would use the following (ignoring the quotes)
\(\d.*\d\) to find "(12-34-23) cat (239)" in a string (12-34-23) cat (233) the .* is greedy and gobbles everything between the (1 and 9). the ? after the .*, i.e. .*? would be non-gready and would match (12-34-23) and (239) without including the cat. In LO you use [:digit:] in place of \d.

Urmas, if you have "(12-34-23) cat (239)" in a cell in calc and search for \([:digit:].*?[:digit:]\) and replace with ww are you saying you get "ww cat ww" in the cell, ignoring "", i.e. 2 substitutions.
Steve

I am trying to find and replace some text with regex in calc. Can anyone help with how I make it non-greedy.

I think the answer is that you don't: you must find a way of constructing a suitable expression despite such expressions being greedy.

I tried \([:digit:].*?[:digit:]\) in place of \([:digit:].*[:digit:]\) but both are greedy. When the following (12-34-23) cat (233) is in a selected cell it replaces both instances between ().

By the way, I don't understand your ".*?" - do you perhaps mean either ".*" or ".?" ?

I'm afraid you've fallen into the common trap of attempting to define your problem by quoting only expressions that *don't* work! Which parenthesised string do you want to match? I'm guessing the first.
But what exactly are the characteristics of the strings you want to match? And the ones you want not to match?

Is the target string always at the beginning? Try:
\<\([^\)]*\)

Is the target string always digits-hyphen-digits-hyphen-digits? Try:
\([:digit:]+-[:digit:]+-[:digit:]+\)
or:
\(\d+-\d+-\d+\)

Do you want to match the second string instead? Try:
\([:digit:]+\)
or:
\(\d+\)

Depending on exactly what you want to match and to avoid matching, you may have to do something different.

Sorry I am not that clear now I read it again. In non-LO regex I would use the following (ignoring the quotes)
\(\d.*\d\) to find "(12-34-23) cat (239)"

You are now saying you want to match the entire string - but I suspect you don't mean that.

... in a string (12-34-23) cat (233) the .* is greedy and gobbles everything between the (1 and 9). the ? after the .*, i.e. .*? would be non-greedy and would match (12-34-23) and (239) without including the cat.

So you actually want to match *both* parenthesised strings but not whatever else there is? Is that no matter what is in the parentheses? This is still not a defined problem.

In LO you use [:digit:] in place of \d.

No: you can now use \d as well.

If the parentheses can contain anything (except parentheses), try:
\([^\)]*\)

This will need two finds to find two parenthesised strings, but if you are using Replace All that won't matter.

Brian Barker

Hi

Your supposedly non-greedy match is definitely non-greedy here. I have
to hit "Replace" twice on your example input.

What version of LibreOffice are you using? Non-greedy matches are
supported only from 4.0 upwards. So if you are still on 3.x line, then
you are out of luck (or simply upgrade, if you may).

Also, although it's not a case here, it's worth to note that Find and
Replace in Calc will find and select entire cells only. That means, if
only part of cell matches regexp, you will end up with entire cell
selected anyway. If you happen to test your regexpes with "Find" first,
it might lead you to believing your regexp is not working properly.
If in doubts, check your regexpes in Writer.

Thanks. Yes, I am still on 3.x series. I have not converted (and checked) yet the files I have that 4.x onwards dropped support for and can't update until I have done so.
steve

For what it's worth, on Linux you can install 3.x in parallel with 4.x.
In fact, each line (3.4, 3.5, 3.6, 4.0, 4.1 etc.) is installed in it's
own directory.

On Windows, you may use server installation GUI [0] to install one
branch alongside another.

Either way, the most important thing is that you can't have two
versions running in parallel (including quickstarter, if you are using
it). But they can be installed and run sequentially.

So, you can upgrade for usual daily work and keep 3.6 installed for
better times, when you feel like converting your old documents.

[0] http://flosmind.wordpress.com/libreoffice-server-install-gui/