9

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 problematic. Basically just somehow sum the current number with +100.

I have the following lines

<devio1="875" devio2="7779" devio3="5635" devio4="154"/>
<devio1="765" devio2="74779" devio3="31535" devio4="544"/>
<devio1="4335" devio2="13" devio3="55635" devio4="1565"/>

By using this regular expression with Notepad++

<devio1="([0-9]+)" devio2="([0-9]+)" devio3="([0-9]+)" devio4="([0-9]+)"/>

I can find all the numbers inside the "" but I cannot find a way to add +100 to all of them. Can this task be achieved with Notepad++ using Regular Expressions?

user3088879
  • 121
  • 1
  • 1
  • 5
  • 1
    Not really an answer to your question, only to your problem: You may want to consider using Perl. – Peter Dec 10 '13 at 22:51
  • 2
    Regular expressions are for string manipulation, they really aren't suited for doing math. – p.s.w.g Dec 10 '13 at 22:52
  • You can't achieve that using only regex, you'll need a callback. You either write your own plugin for notepad++ or just use your favorite language. – HamZa Dec 10 '13 at 22:52
  • it would be a very easy task for vim. if you want to do it in an editor, choose the right one. – Kent Dec 10 '13 at 23:01

3 Answers3

17

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"/>
    
psxls
  • 6,807
  • 6
  • 30
  • 50
  • 1
    Thanks dude. You are the real mvp! ^^ – Korbi Sep 22 '15 at 12:33
  • Great solution! However, in the later versions of Notepad++, it is not available through the `plugin manager`, so you can use this alternate way to install the python script plugin: https://community.notepad-plus-plus.org/topic/17256/guide-how-to-install-the-pythonscript-plugin-on-notepad-7-6-3-7-6-4-and-above – varun Jan 02 '20 at 18:57
4

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.

HexedAgain
  • 1,011
  • 10
  • 21
0

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.

Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106