I had already developed the code to extract a string between two separators, but I had underestimated your question.
I had not grasped the importance of the request to start from the current position. And this changed everything, because I realized that my code always started from the beginning of the document, not from the current position.
Fortunately, the format of the findNext libreoffice method has a parameter to solve the problem. Answering your question has also served me in the future
This test code defines two different searchDescriptor, the first to simulate the starting point (the string “qqq”), the rest of the code to find all the instances of "ABC. * XYZ with another searchDescriptor
Using the test text
XYZthis is a 1ABC test
qqq
test test test XYZthis is a test 2ABC test test test
XYZthis test test test is a 3ABCtest test test
XYZthis test test test is a 4ABCtest test test
you should only find the 2,3,4 strings but not the 1 that precedes qqq
The code is
sub test()
d = ThisComponent
oS0= d.createSearchDescriptor
oS0.SearchString="qqq"
fnd0 = d.findFirst(oS0) ' fnd0 textCursor pointing to new start of search
oS1= d.createSearchDescriptor ' search all string between XYZ and ABC
oS1.SearchString="XYZ(.)*ABC"
oS1.SearchRegularExpression = True
oS1.searchAll = False
fndq = d.findNext(fnd0.end,oS1) ' the trick, the first parameter of findNext
Do while NOT IsNull(fndq)
msgbox (fndq.string)
fndq = d.findNext(fndq.end,oS1)
loop
end sub
Warning:
- this code works only with strings also consisting of several lines but belonging to the same paragraph, I don’t know how to find strings that start in a paragraph and end up in another paragraph,
The search strings “XYZ(.|\n)*ABC” or “XYZ(.|$)*ABC” for example don’t work
- In this moment I don’t know how to use the backreferences (portions of the regex search string in parentheses) to select only the text between the separators, my code for now also selects the separators.
In the coming days, if you want, I’ll try to solve this problem too, but if other nicks already have the solution, better