title: LibreOfiice Basic function to perform regular expression replacement
body: <h5>Requirement</h5>
  <p>I wish to manipulate string values in a LibreOffice Basic macro using regular expression <strong>replacement</strong>? I should like to use something like <code>y = regex (x, &quot;^¿(.*)\?$&quot;, &quot;$1&quot;)</code> to operate on string variables (rather than cells of a document). The macro is to be used from LibreOffice Calc.</p>
  <p>This is not for the above (trivial) problem, but a possibility that I frequently miss.</p>
  <h5>Apparent status</h5>
  <h6>Only search exposed to scripts?</h6>
  <p>Of course the LibreOffice engine performs regular expression replacement (both in interactive Find/Replace and in the <code>REGEX</code> function). However, it seems that only regular expression <strong>search</strong> is exposed to Basic – and that is not what I am looking for. (It is provided by the somewhat cumbersome <a href="https://www.openoffice.org/api/docs/common/ref/com/sun/star/util/TextSearch.html" rel="nofollow noreferrer">TextSearch</a> service, which (if I recall correctly) can also search in string values in Basic variables.)</p>
  <h6>Spreadsheet functions: do not work</h6>
  <p>I thought one was meant to be able to call Calc functions in LO Basic, but calls like</p>
  <pre><code>z   = regex (x, &quot;^¿(.*)\?$&quot;, &quot;$1&quot;)
  z   = regex (x, &quot;^¿(.*)\?$&quot;, &quot;$1&quot;, &quot;g&quot;)
  </code></pre>
  <p>just said “Basic runtime error. Sub-procedure or function procedure not defined”.</p>
  <h6>Hack</h6>
  <p>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 <em>in extremis</em>!</p>
  <h5>Various sources</h5>
  <p>The question <a href="https://stackoverflow.com/questions/25615052/using-regex-in-a-libreoffice-calc-macro-to-extract-text-from-parentheses-in-a-ce#25628453">Using regex in a libreoffice calc macro to extract text from parentheses in a cell</a> is about using REs to find text in cells.</p>
  <p>Searching <a href="https://api.libreoffice.org/docs" rel="nofollow noreferrer">the API documentation</a> for “regular” only threw up the <a href="https://www.openoffice.org/api/docs/common/ref/com/sun/star/util/TextSearch.html" rel="nofollow noreferrer">TextSearch</a> Service mentioned above.</p>
  <p>Several forums have posts like <a href="https://forum.openoffice.org/en/forum/viewtopic.php?f=20&amp;t=19149" rel="nofollow noreferrer">this</a> that only address search; I have found nothing about replacement.</p>