finding & replacing text with new lines in python

I’m using Version: 6.4.6.2 Build ID: 1:6.4.6-0ubuntu0.20.04.1

I need to find & replace strings in a document that have a new line.

I’m using document.getText().String to get the text for the document then doing a regex for signature lines using the pattern: r"(____[\_]+)\n([A-z0-9\-]+)\s+([A-z0-9\ \-]+)\n"

It finds lines like this:

__________
First Last

The end goal is to have it replace with something like:

First_____
First Last
# Failed
replace = document.createReplaceDescriptor()
replace.SearchRegularExpression = True
replace.SearchString = r"____\nFirst"
replace.ReplaceString = r"WORKS____\nWORKS"
document.replaceAll(replace)


# Failed
replace = document.createReplaceDescriptor()
replace.SearchRegularExpression = True
replace.SearchString = r"____\\nFirst"
replace.ReplaceString = r"nWORKS____\\nWORKS"
document.replaceAll(replace)

# Failed
replace = document.createReplaceDescriptor()
replace.SearchRegularExpression = False
replace.SearchString = r"____\nFirst"
replace.ReplaceString = r"Works____\nFirst"
document.replaceAll(replace)

# Failed
replace = document.createReplaceDescriptor()
replace.SearchRegularExpression = False
replace.SearchString = r"____\nFirst"
replace.ReplaceString = r"Works____\nFirst"
document.replaceAll(replace)

I found this post Find character and replace with a new line but I don’t know how to access the dialog, and on top of that doesn’t find (____[\_]+)\nFirst or ____[\_]+\nFirst

Have you line break or paragraph break?

I’m not 100% sure. I’ve attempted with \p, \\p \u000A \\u000A. document.getText().String shows \n. I’ve even gone as far as to copy/paste in the find/replace dialog itself - ctrl+h - and that doesn’t even work with regex turned off. I forgot to mention I’m using Version: 6.4.6.2 Build ID: 1:6.4.6-0ubuntu0.20.04.1 Even tried replace.SearchString = re.escape(foo)

you can check with the “pi” icon next to ABC in menu bar toggled on, (or CTRL + F10) this will make the line endings visible.
a “pi” character indicates a new paragraph, an arrow left is a line break

The replace object has also an SearchStyles method, and a SearchAttributes method, but i could not get it to work (values obtained from a basic recording)

It’s a pi character vs a left arrow - the kind you see on the enter key. Which when I copy/paste I see an image that looks like the one on the enter key. On top of that Ctrl+F10 doesn’t toggle. I did a few tests and find ___\pFirst doesn’t work either.

I’ve upgraded to 7.0.3.1 and 1:7.0-3-0ubuntu0.20.04.1 and tried the find of ___\pFirst and ____\nFrist sill doesn’t find it. Copy/Paste and turning regex off in the dialog off also doesn’t work.

Good news the Alternative Find & Replace works though __\PFirst is found. It’s a start. Now I just need to figure out how to get python to populate & use that dialog. Then figure out how to install it on the server …