我想增加像这样写的数字:
add(1 )
或add( 1)
,但不是像这样add(1)
。我有一个可以在带有 Python 脚本插件的 Notepad++ 中运行的代码,但它会增加所有数字:
import re
def calculate(match):
return '%s' % (str(int(match.group(1)) + 1))
editor.rereplace('(\d+)', calculate)
add(1 )
另外,如果知道如何在仅、仅add( 1)
、仅情况下增加数字,那就太好了add(1)
。你可以给我推荐任何软件,而不是特别的 Notepad++。
答案1
将脚本改成:
import re
import random
def calculate(match):
return '%s' % (str(int(match.group(1)) + 1))
editor.rereplace('((?<=add\( )\d+(?=\))|(?<=add\()\d+(?= \)))', calculate)
正则表达式解释:
( # group 1
(?<=add\( ) # positive lookbehind, make sure we have "add( " (with a space after parenthesis) before
\d+ # 1 or more digits
(?=\)) # positive lookahead, make sure we have a closing parenthesis after
| # OR
(?<=add\() # positive lookbehind, make sure we have "add(" (without spaces after parenthesis) before
\d+ # 1 or more digits
(?= \)) # positive lookahead, make sure we have a space and a closing parenthesis after
) # end group 1
输入如下内容:
add(1 ) or add( 1), but not like this add(1)
它将给予:
add(2 ) or add( 2), but not like this add(1)