Community
    • Login

    Bug when regex matches start and replacing with a blank

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    bug
    4 Posts 3 Posters 2.9k Views 1 Watching
    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 Offline
      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 Offline
        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 Offline
          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 Offline
            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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors