• Login
Community
  • Login

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

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 2 Posters 864 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.
  • R
    Reggy guy
    last edited by Jan 2, 2020, 12:49 PM

    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
    • R
      Reggy guy
      last edited by Jan 2, 2020, 12:51 PM

      @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!

      A 1 Reply Last reply Jan 2, 2020, 1:14 PM Reply Quote 1
      • A
        Alan Kilborn @Reggy guy
        last edited by Jan 2, 2020, 1:14 PM

        @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. :-)

        R 1 Reply Last reply Jan 13, 2020, 12:37 PM Reply Quote 3
        • R
          Reggy guy
          last edited by Jan 2, 2020, 2:33 PM

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

          1 Reply Last reply Reply Quote 2
          • R
            Reggy guy @Alan Kilborn
            last edited by Jan 13, 2020, 12:37 PM

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

            A 1 Reply Last reply Jan 13, 2020, 5:51 PM Reply Quote 0
            • A
              Alan Kilborn @Reggy guy
              last edited by Jan 13, 2020, 5:51 PM

              @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.

              A 1 Reply Last reply Jan 13, 2020, 11:57 PM Reply Quote 2
              • A
                Alan Kilborn @Alan Kilborn
                last edited by Alan Kilborn Jan 13, 2020, 11:58 PM Jan 13, 2020, 11:57 PM

                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
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors