Search word an select text from current position to found word via macro

Hi there.

I have a very special question. I recorded some macro to quick search some “marker-characters” in a very large text.
This works very well.

But now I would like to add functionalyity to this macros that selects the text between the current position in the text to the found-position of the searched marker word.

Example:
For the following text I use “Macro 1” to find the position of XYZ and then I want to use “Macro 2” to find ABC and at the same time select (and set italic) the text between XYZ and ABC.

Lorem ipsum dolor sit amet,
consectetuer adipiscing elit. Aenean
XYZ commodo ligula eget dolor ABC.
Aenean massa. Cum sociis natoque
penatibus et magnis dis parturient
montes, nascetur ridiculus mus.

Is this possible? Any help?

Thank you

Regards,
BeSt

I think I can answer you this evening, I have to test the code first and I can’t to do now

That would be great. Thank you.

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:

  1. 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

  1. 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

Hi torreone

Thank you for your hint.
With your code I created this code:

Dim SearchDesc As Object
Dim Doc As Object

Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor
SearchDesc.SearchString="„"
SearchDesc.SearchSimilarity = True
SearchDesc.SearchSimilarityAdd = 2
SearchDesc.SearchSimilarityExchange = 2
SearchDesc.SearchSimilarityRemove = 2
SearchDesc.SearchSimilarityRelax = False
Found = Doc.findFirst (SearchDesc)

Dim SearchDescRegExp As Object	
SearchDescRegExp = Doc.createSearchDescriptor
SearchDescRegExp.SearchString = "„(.)*“"
SearchDescRegExp.SearchRegularExpression = True
SearchDescRegExp.searchAll = False

Found = Doc.findNext(Found.end,SearchDescRegExp)

Do Until IsNull(Found)
	Found.CharPosture = com.sun.star.awt.FontSlant.ITALIC
	Found = Doc.findNext( Found.End, SearchDescRegExp)
Loop

This is nearly what I wanted…
The last “problem” is, that this code always starts at the very beginning of the document, but I want the code to start at the “current cursor position”.

Is there any possibility to “create” a com::sun::star::uno::XInterface with the current cursor position to put into findNext-method?

Thank you!!!

Ragards,
BeSt

If by current position you mean the viewcursor position, just avoid the initial search (not by chance I was talking about “simulating the current position” as my first search for my code and use viewcursor as start point.

It is not clear to me which separators you use in your search and why you did not use a single regex that looked for the string between the two separators in a single operation (I am convinced that the backreference can be used not to include the two separators in the search, but these days not the time to investigate the problem

I don’t know how to create the interface of which you speak

This regex pattern

(?<=XYZ).*?(?=ABC) 

detect and extract only the string between separator XYZ and ABC without include XYZ and ABC