Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Regex to change numbers to a value 1 step lower

    Help wanted · · · – – – · · ·
    2
    2
    2475
    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.
    • Pablo Martinez
      Pablo Martinez last edited by

      i have a regex that i use that changes numbers and increases the value, so if theres a 1 in my txt i turns it into 2, a 0 to 1 and so. Ive been trying to make a new regex that decreases value by 1, so it would turn a 4 into a 3, a 9 into an 8 and so on. But im stuck i cant figure it out, ill post the current regex i use and some examples
      find
      (0)|(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)

      replace with
      (?{1}1)(?{2}2)(?{3}3)(?{4}4)(?{5}5)(?{6}6)(?{7}7)(?{8}8)(?{9}9)(?{10}0)

      Now im wondering if its possible to use this same formula but to decrease the numbers

      examples

      james66

      fire1

      dog 2

      lier9

      to

      james55

      fire0

      dog1

      lier8

      1 Reply Last reply Reply Quote 0
      • guy038
        guy038 last edited by

        Hello, @pablo-martinez,

        I’ve just missed your post, till now ! Really easy, indeed !

        The search regex is unchanged (0)|(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)

        The replacement regex becomes (?{1}9)(?{2}0)(?{3}1)(?{4}2)(?{5}3)(?{6}4)(?{7}5)(?{8}6)(?{9}7)(?{10}8)

        Remark : I noticed that, in your previous regex, when you add 1 to digit 9, you get the 0 digit ( part (?{10}0), at the end of your previous replacement regex ).

        So, to stay coherent, with that method, now, if you subtract 1 from 0, you’ll get the number 9 ( part (?{1}9), at the beginning of the new replacement regex ! )

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Copyright © 2014 NodeBB Forums | Contributors