Limiting regex search/replace to selected text in document

I have this macro function for deleting strings that works:

Function findDeleteRegex( regex as string ) As Long
REM Returns: the number of found/replaced occurrences.
    Dim oReplace as Object
    oReplace = ThisComponent.createReplaceDescriptor()
    oReplace.SearchRegularExpression = True
    oReplace.setSearchString( regex )
    oReplace.setReplaceString( "" )
    findDeleteRegex = ThisComponent.replaceAll( oReplace )
End Function

Now I need to limit it to only selected text in the document just as can be done be checking a box in the dialog box that opens when in the document “Edit => Find and Replace …” is navigated to. In this documentation:

https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XReplaceDescriptor.html

I do not see a way to do it. Can this be done?

The options are listed in LibreOffice: SearchDescriptor Service Reference. Honestly, I have always hated those reference pages, because they aren’t really informative.
Replacing in selected text only is apparently not an option here, so you may have to set the searchable text variable to the current selection instead of the entire document.

You may look for a simple workaround:

import re

def delete_regex_in_selection(regex):
    doc = XSCRIPTCONTEXT.getDocument()
    selections = doc.CurrentSelection
    rex = re.compile( regex )
    for selection in selections:
        selection.String = rex.sub("", selection.String)

def main(*_):
    delete_regex_in_selection("<your regex here>")

install apso.oxt to organize python