Notepad++ Regular Expression add up numbers

前端 未结 3 1243
一个人的身影
一个人的身影 2020-12-11 04:22

How do I sum or add a certain value to all those numbers? For example my goal is to increase all those numbers inside the \"\" with 100 but achieving that has been problemat

相关标签:
3条回答
  • 2020-12-11 04:54

    That's not possible with the sole use of regular expressions in Notepad++. Unfortunately there's no way to perform calculations in the replacement pattern.

    So the only way of accomplishing your task in Notepad++ is with the use of the Python Script plugin.

    1. Install Python Script plugin from the Plugin Manager or from the official website.
    2. Then go to Plugins > Python Script > New Script. Choose a filename for your new file (eg add_numbers.py) and copy the code that follows:

      def calculate(match):
          return 'devio%s="%s"' % (match.group(1), str(int(match.group(2))+100))
      
      editor.rereplace('devio([0-9])="([0-9]+)"', calculate)
      
    3. Run Plugins > Python Script > Scripts > add_numbers.py and your text will be transformed to:

      <devio1="975" devio2="7879" devio3="5735" devio4="254"/>
      <devio1="865" devio2="74879" devio3="31635" devio4="644"/>
      <devio1="4435" devio2="113" devio3="55735" devio4="1665"/>
      
    0 讨论(0)
  • 2020-12-11 05:01

    I'm not really familiar with notepad++ but for an algorithm, supposing you have a number abcd = a*1000 +b*100 + c*10 + d, then so long as b is in [0,8] you can just replace b by b+1. As for when b = 9 then you need to replace b with 0 and replace a with a+1 (and if a = 9 then you'd replace a by 10).

    Noting this, you could then, for three and four digit numbers, say, apply the following regexes:

    \([1-9]+\)0\([0-9]{2}\) -> \1 1\2, 
    \([1-9]+\)1\([0,9]{2}\) -> \1 2\2, 
    ... -> , 
    \([1-9]+\)8\([0-9]{2}\) -> \1 9\2, 
    

    and so on ... Noting that you also have to consider any a=9, b=9 integers, and larger integers; this suggests some sort of iteration with if statements covering the cases where the coefficients of 10^x (x>=2) are equal to 9. When you start actually coding this (or doing it by hand) you will begin to realize that doing this with a pure regex approach is going to be painful.

    0 讨论(0)
  • 2020-12-11 05:01

    Regex doesn't support arithmentic and Notepad++ doesn't support any computation beyond regex, so you're stuck if you're limiting yourself to that tool. There are, of course, many other non-Notepad++ solutions, some of which are discussed in Math operations in regex.

    0 讨论(0)
提交回复
热议问题