Regex] Rounding numbers python script does not run properly

  • Hi,

    unfortunately i can’t comment on the thread , so i’m asking my question here.

    First of all I want to thank @PeterJones for the nice Python script for rounding numbers in Notepad++.

    However, the script does not run correctly for unknown reasons. The following series of numbers (coordinates):

    5494577.00001803319901228 5650290.99997399840503931, 5494577.00001803319901228 5650288.99997399933636189, 5494575.00001802947372198 5650288.99997399933636189, 5494575.00001802947372198 5650286.99997399840503931, 5494573.00001803319901228 5650286.99997399840503931

    is converted to:

    5494577.0000 5650290.99997399840503931, 5494577.0000 5650288.99997399933636189, 5494575.0000 5650288.99997399933636189, 5494575.0000 5650286.99997399840503931, 5494573.0000 5650286.99997399840503931

    Unfortunately I could not fix the error in the script. What is exactly the problem?

    Thank you in advance!



  • @unfassbarstephan

    the issue might be the recalculation of the next start point.
    May I propose a different approach?

    editor.rereplace("-*\d+\.\d{4,}", lambda m: round(float( , 3))

    Just this line, nothing else.
    rereplace takes care that the next start position
    is calculated according to the replaced text.

  • @Ekopalypse

    When I try the one-liner on the user’s data, I get this as a result (I don’t think it is the intent?):

    5494577.0 5650291.0, 5494577.0 5650289.0, 5494575.0 5650289.0, 5494575.0 5650287.0, 5494573.0 5650287.0

    But maybe it is…it would have been nice if the OP had said exactly what they were trying to achieve!

  • @Alan-Kilborn

    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(, 3)))

  • @Ekopalypse

    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 to editor1 ? Methinks you might be too focused on which view is active. I pretty much ignore editor1 and editor2 in my programming unless I really need them. :)

  • @Alan-Kilborn

    :-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(, 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)


  • …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?

  • @unfassbarstephan

    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?

  • @PeterJones

    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


    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: in the previous post, but it is too late for an edit. :(

  • @Alan-Kilborn

    one idea might be to search for matches and replace only those which are
    in the same range as the selected text. Something like

    if start_pos_selected text <= match_start and end_pos_selected_text >= match_end:

    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.

  • @Alan-Kilborn

    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…?

Log in to reply