Delete lines matching regex programmatically

I’m trying to delete whole lines from a Writer document which match a pattern like ‘%C%c…%c%C’.
I’m using construct like this:

xTextDocument = (XTextDocument)xComponent;
XReplaceable xReplaceable = (XReplaceable)xTextDocument;
XReplaceDescriptor xReplaceDescriptor = xReplaceable.createReplaceDescriptor();

{
	XPropertySet replaceProps = xReplaceDescriptor;	
    string cStringRegex = @"%C[\s\S]*?%c[\s\S]*?%c[\s\S]*?%C$";
    replaceProps.setPropertyValue("SearchRegularExpression", new uno.Any(true));
    xReplaceDescriptor.setSearchString(cStringRegex);
    xReplaceDescriptor.setReplaceString(string.Empty);
    xReplaceable.replaceAll(xReplaceDescriptor);
}

I have the following issues:

  1. Code above leaves blank lines
    behind, and I don’t want to remove
    any more pre-existing blank lines.

  2. This pattern does match multiline strings from LibreOffice Writer UI, but doesn’t seem to work programmatically.

How do I get it to solve the above conditions?

[PS: I just read about balancing groups in C# Regex, but does LibreOffice have support for this?]

Wouldn’t your regexp be written in a simpler way by replacing [\s\S] with .?

You don’t delete lines because lines do not exist as primary objects. They are only a result of distributing text onto the sheet. Line wrapping creates the visual lines.

$ in a regexp is a position located just before a paragraph marker. It is not the paragraph marker itself.This means the paragraph marker is not captured and can’t be replaced. If your replacement results in an empty paragraph, this empty para is left behind and nothing distinguishes it form intentional empty paragraph. But note that empty paragraphs are faulty (this a direct formatting for vertical space). Vertical space should be specified in the paragraph style attributes. When this is done properly, the document contains no empty paragraph and empty paragraphs can then be eliminating by searching for ^$ and replacing with nothing.

I wrote [\s\S] because it would also capture '\n', '\r', etc characters I was told, which '.' wouldn’t? I understood about ‘vertical space’, so I need to reformat my document to use '\v' style character? Also, this is very strange, xTextDocument.getText().getString() returns "\r\n" line endings.

. stands for “any character”. It thus captures also \n and \r. But these characters never appears in a well-behaved Writer document anyway because line ends are not encoded in the document but are dynamically generated as the document is displayed.

Paragraph markers are themselves “off-text” information internally managed by Writer. Only paragraph content text is visible.

Similarly, vertical spacing is off-text data associated with a paragraph. Don’t add \v characters lest you create a real mess making your document un-formattable. Vertical spacing is one of the attributes of paragraph styles. You can tune it in the Indents & Spacing tab of the style, e.g. Text Body.

Read the Writer Guide for basic information about styles.

I have never used getString but if it returns \r\n, then you likely typed Shift+Enter instead of Enter, which created linebreaks instead of paragraph ends.