Why is it that some regex match is not replaced one by one?



  • I am using notepad++ for text editing.

    sample text : hello,this is a test.are you ready?

    search for : (?<=[a-z,.?! ])([,.?!])(?=[a-z])

    replace with : \1

    replace result : hello, this is a test. are you ready?

    It works as intended, only if I replace them all, but if I click on replace button, nothing happens. It navigates to the next match as if I click on find.

    refer to this screenshot [https://i.stack.imgur.com/4CiPY.png]

    Is it a bug and what’s the solution I should try?

    Thank you!



  • @katovic-bar ,

    Any regex that uses \K match-resets or (?<=...) / (?<!...) lookbehinds require using replace all rather than single replace actions. That’s the way it was designed, to avoid certain …confusions… that would arise with reset- and lookbehind- replacements run one-at-a-time.



  • thanks for the reply. maybe this is the alternative I can think of.

    ([a-z,.?! ][,.?!])(?=[a-z])

    it’s not perfect, but it does the job anyway :)
    only downside is it matches preceding characters as well, so it looks less charming.



  • can I ask one more for troubleshooting?
    I wonder if I could open the document and remember the last scroll position so next time I reopen it, it straight nagivgates to where it was.



  • @katovic-bar

    remember the last scroll position

    if you close/exit Notepad++ and reload the application, it does remember the scroll position for the active session. But no, once you close the document, it loses that scroll information.



  • @katovic-bar said in Why is it that some regex match is not replaced one by one?:

    I wonder if I could open the document and remember the last scroll position so next time I reopen it, it straight nagivgates to where it was.

    You may find THIS to be of interest on that topic. There’s a Pythonscript there that supposedly solves this “problem”. I haven’t tried the script out yet, so I can’t fully endorse it. It’s on my evergrowing “TODO” list. :-)


Log in to reply