Requirement
I wish to manipulate string values in a LibreOffice Basic macro using regular expression replacement? I should like to use something like y = regex (x, "^¿(.*)\?$", "$1")
to operate on string variables (rather than cells of a document). The macro is to be used from LibreOffice Calc.
This is not for the above (trivial) problem, but a possibility that I frequently miss.
Apparent status
Only search exposed to scripts?
Of course the LibreOffice engine performs regular expression replacement (both in interactive Find/Replace and in the REGEX
function). However, it seems that only regular expression search is exposed to Basic – and that is not what I am looking for. (It is provided by the somewhat cumbersome TextSearch service, which (if I recall correctly) can also search in string values in Basic variables.)
Spreadsheet functions: do not work
I thought one was meant to be able to call Calc functions in LO Basic, but calls like
z = regex (x, "^¿(.*)\?$", "$1")
z = regex (x, "^¿(.*)\?$", "$1", "g")
just said “Basic runtime error. Sub-procedure or function procedure not defined”.
Hack
As a horrible hack, one could manipulate some cells containing appropriate formulae in a scratch part of my sheet, but that has so many evident disadvantages that I would only use it in extremis!
Various sources
The question Using regex in a libreoffice calc macro to extract text from parentheses in a cell is about using REs to find text in cells.
Searching the API documentation for “regular” only threw up the TextSearch Service mentioned above.
Several forums have posts like this that only address search; I have found nothing about replacement.