Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

regex - Notepad++ Regular Expression add up numbers

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"/>
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...