I have a text file that is line wrapped. The first line in each block ends in a tilde (~) This is an example:
Now is the time for all~
good men to come to the
aid of their party.
The quick brown fox~
jumped over the lazy
What light through~
yonder window breaks?
I need it to come out looking like this:
Now is the time for all good men to come to the aid of their party.
The quick brown fox jumped over the lazy yellow dog.
What light through yonder window breaks?
I recorded a macro that will do it, but I have to run the macro manually for each line.
My question is, how can I wrap this file together in Python, either by running the macro in a for() loop until it finishes or entirely in Python?
The macro I recorded does:
- regex find and select "~\R([^~]*)"
- Scintilla command 2303 LineUpExtend
- Notepad command "Edit Join Lines"
- Scintilla command 2304 CharLeft
- Scintilla command 2180 Clear
O.K., this is what I came up with:
from types import *
# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
done = 0
while (done != 1):
endPos = editor.getLength()
firstTilde = editor.findText(0,0,endPos,"~")
secondTilde = editor.findText(0,firstTilde,endPos,"~")
if (type(firstTilde) == NoneType):
if (type(secondTilde) == NoneType):
done = 1
notepad.runMenuCommand("Edit", "Join Lines")
# End the undo action, so Ctrl-Z will undo the above two actions
# Turn the undo recorder back on.
Constructive critism and python tips appreciated.
It's a bit late to answer, isn't it ? But never mind !
I think it can, also, be done with a search/replacement in regular expression mode !
What are we looking for ? :
If there is a dot, eventually followed with a tilde (~), before the EOL character(s), we just have to re-copy the dot, followed with the EOL character(s).
If NOT, we must change any EOL character(s), eventually preceded by a tilde (~), with ONE space (\x20).
Then the Search/Replacement could be something like below :
SEARCH : ((\.)|)~?(\R)
REPLACE : (?2.\3: )
In Search, there's an alternative that means a single DOT OR Nothing, followed with a possible tilde, followed by any kind of EOL ( \r\n for Windows, \n for Unix or \r for Old Mac )
In Replacement, there's a conditional form that means : if Group 2 exists ( the DOT ) then rewrite it, followed with the Group 3 ( \R ). If NOT, ( part after the colon ) just write a single space
Tested with the 6.5.1 version of N++ => Everything fine :-)
Enjoy our loved editor,
You can find good documentation, about the new PRCE Regular Expressions, used by N++, since the 6.0 version, at the TWO addresses below :
The FIRST link concerns the syntax of regular expressions in SEARCH
The SECOND link concerns the syntax of regular expressions in REPLACEMENT
So, four months later, I just want to say that those are excellent links on PCRE. Also, that's a pretty insightful use of regex to solve the problem.
Sign up for the SourceForge newsletter:
You seem to have CSS turned off.
Please don't fill out this field.