Community
    • Login

    Regex to increase or decrease last digit in line by one.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 2 Posters 1.6k 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.
    • Reggy guyR Offline
      Reggy guy
      last edited by

      So I want to know if there is a regex to increase or decrease the value of the last digit in a line by one.
      Example:

      Reg1ex234 >>>> Reg1ex235

      I’ve seen several examples to change ALL the digits in a line, but not the last one. Is this possible by regex only?

      1 Reply Last reply Reply Quote 0
      • Reggy guyR Offline
        Reggy guy
        last edited by

        @guy038 I saw your reply https://community.notepad-plus-plus.org/topic/14884/regex-to-change-numbers-to-a-value-1-step-lower/2

        Is this possible only for the last digit in the line via regex? Thank you!

        Alan KilbornA 1 Reply Last reply Reply Quote 1
        • Alan KilbornA Offline
          Alan Kilborn @Reggy guy
          last edited by

          @Reggy-guy

          If your intent is to only ever affect the last digit, then yes, perfectly possible. But…most people would expect a change of this sort to transform 2019 into 2020 (note that TWO digits changed!) and this is not a reasonable expectation from regex replacement.

          However, if that’s acceptable, then a way to change the only the last digit appearing on a line to the next digit lower is to search for (?-s)^(.+)(?:(0)|(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)) and replace with \1(?{2}9)(?{3}0)(?{4}1)(?{5}2)(?{6}3)(?{7}4)(?{8}5)(?{9}6)(?{10}7)(?{11}8)

          That borrows heavily from the other post you linked to. :-)

          Reggy guyR 1 Reply Last reply Reply Quote 3
          • Reggy guyR Offline
            Reggy guy
            last edited by

            Thank you very much @Alan-Kilborn . It will be enough to resolve my current problem :).

            1 Reply Last reply Reply Quote 2
            • Reggy guyR Offline
              Reggy guy @Alan Kilborn
              last edited by

              @Alan-Kilborn Hello again, could you help me with the regex to do this to the first number in every line instead of last?

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA Offline
                Alan Kilborn @Reggy guy
                last edited by

                @Reggy-guy said:

                the regex to do this to the first number in every line instead of last

                Deceptively simple, I think.

                Just add a ? after the + in the earlier search expression.

                Alan KilbornA 1 Reply Last reply Reply Quote 2
                • Alan KilbornA Offline
                  Alan Kilborn @Alan Kilborn
                  last edited by Alan Kilborn

                  I should add that it works for this reason:

                  .+ :

                  .+ matches (one or more characters) as much as it can before moving on. Also known as the “maximum munch”. So given the pattern .+\d and the string-to-test as abc123def345 the .+ part would match abc123def34 because the ending 5 would satisfy the \d in the pattern.

                  .+? :

                  .+? matches (one or more characters) as little as possible before moving on. Also known as the “minimum munch”. With the pattern .+?\d and the same string-to-test, the .+? part would match abc because the following 1 would match the \d.

                  1 Reply Last reply Reply Quote 2

                  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