我想增加這樣寫的數字:
add(1 )
or add( 1)
,但不是這樣寫的add(1)
。我有一段程式碼可以在 Notepad++ 中使用 Python 腳本插件運行,但它會增加所有數字:
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)