Regex] Rounding numbers python script does not run properly
-
if it is really needed to show 3 digit zeros then a slight modification will do it. :-)
editor1.rereplace("-*\d+\.\d{4,}", lambda m: '{:0.3f}'.format(round(float(m.group(0)), 3)))
-
Ah. The
.0
or.000
seems like extra noise to me, but maybe it is required for OP’s need (often wish people would explain their need better).But, very nice one-liner Eko! It’s a clip-and-saver.
A switch from
editor
toeditor1
? Methinks you might be too focused on which view is active. I pretty much ignoreeditor1
andeditor2
in my programming unless I really need them. :) -
:-D I constantly keep forgetting this, yes, it should be editor, thx for the head up.
editor.rereplace("-*\d+\.\d{4,}", lambda m: '{:0.3f}'.format(round(float(m.group(0)), 3)))
Yes, to me it looks awkward too but as you said, it might be in the interested of the OP.
-
Just for fun (I say that a lot), here’s a version that does a prompted replace with preview (wouldn’t it be great if N++ itself offered a preview to its [regex] replacement?). Strange but I’ve never coded this kind of thing before for N++; one more thing to stash in the bag-of-tricks…
def main(): while True: match_list = [] editor.research("-*\d+\.\d{4,}", lambda m: match_list.append(m.span(0)), 0, editor.getCurrentPos(), editor.getTextLength(), 1) if len(match_list) == 0: return editor.setSelection(match_list[0][1], match_list[0][0]); editor.scrollCaret() repl_text = '{:0.3f}'.format(round(float(editor.getSelText()), 3)) result = notepad.messageBox('Replace selected text with\r\n\r\n{}\r\n\r\n???'.format(repl_text), '', MESSAGEBOXFLAGS.YESNOCANCEL) if result == MESSAGEBOXFLAGS.RESULTCANCEL: return if result == MESSAGEBOXFLAGS.RESULTYES: editor.replaceSel(repl_text) main()
-
…still having fun with this…so much can be done to make interactive replace much better than what N++ can do; sample:
-
Wow, thank you dir your help! I will try this approach.
-
@Alan-Kilborn said:
…still having fun with this…so much can be done to make interactive replace much better than what N++ can do; sample:
The one-line-solution works like a charm.
Would you also publish your code for the interactive replace? That would be great.
Short question: What does “???” mean?
-
Short question: What does “???” mean?
This is Alan’s wasteful use of the question mark,
for which it is also used here, to ask a question. :-D -
@unfassbarstephan said:
Would you also publish your code for the interactive replace?
I did…for your specific case. I’m still working on a generic version; may never finish it… :(
@Ekopalypse said:
Alan’s wasteful use of the question mark
Wasteful? It doesn’t seem right without it; I did consider it this way as well, maybe better?:
Replace selected text with? :
-
@Alan-Kilborn said:
Replace selected text with? :
Maybe
Would you like to replace the selected text with the following text?
-
Well, Pythonscript sizes the dialog box according to contents, so I was trying to keep it as narrow and short as possible to avoid covering too much of the Notepad++ main window. But…I’ve already added more info to make it taller, so why not wider as well? :)
-
Would you also publish your code for the interactive replace?
I’m still working on a generic version; may never finish it
Follow-up:
There may not be a reasonable way to script this (the “generic version”); here’s why: There doesn’t seem to be a way to do a “boost-ish” replacement option on a Python string. It’s only possible to do it on in-place text.
What I want to do in the code is pull a copy of the matched text into a Python string and then do a Notepad++ -like replacement operation on that. Sure I can do a Python “re” replacement on it, but that isn’t always going to be equivalent.
Digging deeper, I see there is an open issue on this kind of thing in the Pythonscript code pages, here. But it hasn’t been acted on so it isn’t a reality.
If anyone has thoughts on this, I’m all ears. @Ekopalypse ?
-
Ugh. Clearly I meant this Pythonscript issue: https://github.com/bruderstein/PythonScript/issues/101 in the previous post, but it is too late for an edit. :(
-
one idea might be to search for matches and replace only those which are
in the same range as the selected text. Something likeif start_pos_selected text <= match_start and end_pos_selected_text >= match_end: replaceSelText
Or did I misunderstood your goal? :-(
-
@Ekopalypse said:
did I misunderstood your goal?
Maybe that part. :)
To be able to offer a preview of the yet-to-be-done replacement, I need to know what that replacement will be. I can’t do that without actually doing the replacement. Catch-22. I could do it and then undo it, I suppose, but that gets messy. Mostly it should be OK, but sometimes with regexes Boost can differ from Python’s re.
-
what about using a hidden scintilla instance?
-
@Ekopalypse said:
what about using a hidden scintilla instance?
That also sounds “messy” (or “involved”) but if you have some snippet to show me it is not so bad…?
-
I used it here.
-
just be careful about
notepad.destroyScintilla(self.hidden_scintilla)
Currently npp crashes when using this and then request another buffer.
There is already a merged fix but only available on next release. -
This is the issue, I assume: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/4487