Find and Replace Macro with Regular Expression Enable

I am using Ubuntu 22.04 and Libreoffice 7.5. I have created an extension which works when there is tick on Find and Replace - Other Options - Regular Expression.

Suppose there is no tick on Regular expression and I want to search for “\n\n” and replace it with “” what line should I add after this?

REM ***** BASIC *****

sub Exp

RDescrip = ThisComponent.createReplaceDescriptor

RDescrip.searchRegularExpression = True

RDescrip.searchString = “\n\n”

Do you want to replace the empty lines or empy paragraphs, or the doubled empty lines or the doubled empty paragraphs??
Are those lines/paragraphs empty really? (Maybe there are Spaces and/or TABs inside…

Can you upload an ODF type sample file here?

From the Help - the \n means:

A line break that was inserted with the Shift+Enter key combination when in the Find text box.

A paragraph break that can be entered with the Enter or Return key when in the Replace text box in Writer. Has no special meaning in Calc, and is treated literally there.

To change line breaks into paragraph breaks, enter \n in both the Find and Replace boxes, and then perform a search and replace.

Similar topics on the AOO/LO Forum:
https://forum.openoffice.org/en/forum/viewtopic.php?t=31067
https://forum.openoffice.org/en/forum/viewtopic.php?t=89004

I achieved the desired result by find and replace command but there much more things like \n \n I had created macro for that but if there is no tick on regular expression it do not work. So I want a macro for it.

Please upload an ODF type sample file and your full macro code (the full subroutine) here.

Extension Code.odt (37.9 KB)

Please check the odt file

Do you want to remove the two empty paragraphs at the end of the sample document containing the macro code as pure text?
.
Your sample file is very strange.
,
The macro code must be located in a Module of the Standard library, and you must demonstrate in the text body: what you want to remove by the macro from the text…

English Roznama.odt (13.0 KB)
I want to remove empty line from this file. I know how to remove it via find and replace and regular expression enable. I have also created a macro which works when there is tick on find and replace other options regular expression. But I want o create a macro which will work even when there is no tick on regular expression.

Something like this?

Sub findReplaceDoubleShiftEnter
	dim oDoc as object, oDesc as object
	oDoc=ThisComponent
	oDesc=oDoc.createReplaceDescriptor
	with oDesc
		.SearchString=chr(10) & chr(10) '2x Shift+Enter (under OS Windows, under Linux chr(13)?)
		.ReplaceString=""
	end with
	oDoc.replaceAll(oDesc)
End Sub
1 Like

There are Paragraph End marks (ENTERs) and Line Feed characters (SHIFT-ENTERs) in your sample document. And there are single space characters in front of some LF and Paragraph END characters. Those lines ARE NOT empty, because there is one Space character inside!!!

Thank you so much for giving me the idea

I also want to replace this 
"^\n", "^$", "^.$", "^$" 
with ""
	with oDesc
		.SearchString="" 'set what to find
		.SearchRegularExpression=true 'by regexp
		.ReplaceString="" 'set what to replace
	end with
1 Like

Thank you once again