Is it possible to use a regular expression in the Replace box in LibreOffice Calc?

This question has been asked several times in the past with no answer to date.
It seems actually there’s an unresolved issue in LibreOffice Calc that prevents using regular expressions in the Replace box.
For instance, you can enter regex codes for tabs, spaces, linefeeds, etc. (\t, \s, \n) in the Find box but you cannot use them in the Replace box - they are treated literally.
Any ideas or suggestions?

No it is not possible; and using regular expressions in replacement field actually does not make sense: regular expression is a mask that should match something in searched text - something not suitable for replacement text. It doesn’t make sense to use \s there (meaning “insert unspesified spacing character”). It is possible to use \n and back references, though.

What you are asking about is not using regular expressions in replacement field - but rather having ways to express some specific characters there that have dedicated syntax in regular expressions, like unicode codepoints, but aren’t accepted in replace field. You are welcome to file an enhancement request.

See bug report (enhancement request) tdf#106137.

But of course certain regexp can be used in the replace box and they appear to work in calc. e.g. “&” might be useful.The help page tells you which ones work.

“&” isn’t a special character for RegeEx. It’s one of the “special characters” introduced only for the replace string. In addition (afaik) the special usage of the character “$” followed by a decimal digit in OO/LibO is rather uncommon. Mostly the backslash is used in this role like in back-references to groups found in RegEx. The usage of “$” for “end of text” (text being a paragraph in this context in OO) in the search sting also seems to be uncommon. AltSearch uses “\p” for the purpose which seems more “intuitive” to me. To use “$” in the two mentioned very different roles i would consider definitely a bad idea. Even worse is “\n” inserting a paragraph break if used in the replace string.

Sure, but what does make sense is to fill in part of the replacement string based on matched text. Say you have “Figure 5.6” and “Figure 6.6” and the document specification changes so that you need “Example 5.6” etc. You don’t want to replace Figure that is not followed by a numeral, so your search term may be Figure ([0-9]+) and the replacement string, in some editors, could be Figure \1 but LO appears not to support this.

hjh

No. By definition, regular expression is some mask matching a text, so the term only makes sense as something to be searched. In replacement, only some special replacement syntax makes sense, which may happen to have some similar pieces to regular expression syntax, or special things to refer to the matched pieces; but do not use the clearly defined term in a wrong context.

Additionally, you need to refer to the syntax used in LibreOffice. $1 is used to refer to the text of capture group ‘1’, not \1.

LO help describes capture groups (…) as well as special characters \t and \n in the replace box
https://help.libreoffice.org/latest/ro/text/shared/01/02100001.html?&DbPAR=SHARED&System=UNIX

I did not dispute the fact that regexes are not replacement strings.

Thanks for this, and for the LO documentation reference… I was web searching for this answer but could not find it.

hjh

1 Like

Help button is underrated :wink: - clicking it in the Find & Replace dialog would bring you to the page with links to regular expressions in LibreOffice :wink:

1 Like

I have the same problem on Ubuntu 19.10

The character $ detects the end of paragraphs but \n or \t don’t work in Find or Replace.

Please do not use the Answer field for comments that are not an answer to the original question, use add a comment instead. Thanks.

1 Like