Community
    • Login

    Bug when regex matches start and replacing with a blank

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    bug
    4 Posts 3 Posters 2.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Glen EdmondsG
      Glen Edmonds
      last edited by

      The behaviour is as expected when replacing with something, but when replacing the match is from start of input and the replacement is blank another match is allowed as if there’s a new start of input.

      To replicate, use text “aaa,bbb,ccc,—”, search: “^\w+,”, replace: “” (all without quotes)
      Expected result: “bbb,ccc,—”. Actual result: “—”

      If you replace with “#”, it works as expected yielding “bbb,ccc,—”

      1 Reply Last reply Reply Quote 0
      • AdrianHHHA
        AdrianHHH
        last edited by

        If you use “replace” it replaces just one occurrence. If you use “replace all” then it works in the way you describe as a bug. I am not sure that this a bug in Notepad++. I think it is more an issue of understanding regular expressions.

        Given the line “|aaa,bbb,ccc,–” where the “|” denotes the cursor position. The first find matches “aaa,” and replaces it with nothing, leaving “|bbb,ccc,–”. The next find within the “replace all” finds “bbb,” and the replace leaves “|ccc,–”. And it repeats.

        One way around this is to change the search string to be “^\w+,([^\r\n])” and the replacement to be “\1”.

        1 Reply Last reply Reply Quote 0
        • Glen EdmondsG
          Glen Edmonds
          last edited by

          @AdrianHHH I understand regex very, very well. It seems that, unlike every other tool/language with which I am familiar (which is quite a few), after every replacement Notepad++ “resets” the input as if the input for next match is result of the current replacement. This is non-standard behaviour.

          In my example, after “aaa,” is replaced with blank, start of input is “refound” just before “bbb”. AFAIK all other tools would not match this - there is only one start of input.

          1 Reply Last reply Reply Quote 0
          • Scott SumnerS
            Scott Sumner
            last edited by

            I had to get used to Notepad++ regex behavior when I first started using it, too. If you care to read something about that, have a look at “point #3” references in this thread:

            https://notepad-plus-plus.org/community/topic/137/select-by-columns

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors