Find replace Line end \n in a macro

I am trying to find an replace line end \n using a macro (python).
I create a search descriptor via a writer document xDocument.createReplaceDescriptor() method.
I set the SearchRegularExpression propery to True.

No matter what I try I am not able to match a simple string with when I inlcude \n.
A search string of abc\n will not match, but abc$ will match.
The issue I am having with abc$ is the line end is not replaced when I do a replace using the search descriptor.
I want to find a match including the \n so I can replace the find and the line ending.
This has to be done in code for my needs.

For example if I had the following:

I want to do a find and replace using a macro so the result would be:

seems ‘\n’ is a too quick abstraction, and you hit the paragraph boundary,
like recently discussed here :

How about doing three substitutions instead of one? Something like
\n replace with ~ and get 1~a~2~b~3~c
now replace ([^~]*)~([^~]*)~ with $1~$2\n and get


and finally replace ~ with empty string (or with a space)

The issue is the search and replace in macros cant find and replace \n. So your solution of find \n and replace with ~ seems not to work, this is the exact issue I am having.
At least I have not got it to work. For my needs this has to be done in a macro.

Just a guess - the problem is not \n, it should work. The problem is which TRUE Python is trying to set to the SearchRegularExpression. Something similar has already been discussed for Lua.

I have confirmed that regular expression search is working. It is not a boolean issue with UNO and python.

This works for me (in BASIC)

Sub tstReplaceOddCR
Dim oSearchDescriptor As Variant
  oSearchDescriptor = ThisComponent.createSearchDescriptor()
  oSearchDescriptor.SearchRegularExpression = 1
End Sub

Maybe Python immediately turns the string \n into a newline character? Maybe this needs to be strictly stated? I’m not good at Python, but it seems there was some kind of string in triple quotes?

hmm… I will do a little more testing on this. I have tried escaping \\n in python, which is a standard thing to do. So far \n or \\n does not work.
I noticed your search string is set to only "\n" and not "somevalue\n" I will have to try the different varients in basic to confirm they work.

regex_basic_new_line.odt (10.6 KB)

I just put together this super simple example using your macro and I did not get it to work.

I am using LibreOffice 7.6 on Ubuntu 22.04.

Acutally it is working. I was using paragraph breaks and not line breaks.

wow. such a surprise :wink: