srh*_*vmn 8 regex tabs indentation find-and-replace indent
我需要在大量具有许多函数定义的Python文件中替换所有出现的
def some_func(foo, bar):
Run Code Online (Sandbox Code Playgroud)
和
@jit(parallel=True)
def some_func(foo, bar):
Run Code Online (Sandbox Code Playgroud)
无论缩进程度如何def some_func(foo, bar)。
示例:我想更换
def some_func_1(foo, bar):
def some_func_2(foo, bar):
def some_func_3(foo, bar):
def some_func_4(foo, bar):
Run Code Online (Sandbox Code Playgroud)
和
@jit(parallel=True)
def some_func_1(foo, bar):
@jit(parallel=True)
def some_func_2(foo, bar):
@jit(parallel=True)
def some_func_3(foo, bar):
@jit(parallel=True)
def some_func_4(foo, bar):
Run Code Online (Sandbox Code Playgroud)
动机:我想“强力加速/并行化”FDTD 模拟包,而不必通过利用 的自动并行化来重写整个numba代码库@jit。
PS.:也欢迎对这种(ab)使用的幼稚方法提出任何评论/批评@jit(例如,如果这根本不起作用)!
Tot*_*oto 13
这适用于任何类型的空格(空格或制表)和任何类型的换行符\n, \r\n, \r。
^(\h*)(?=def\b.*(\R))$1@jit\(parallel=True\)$2$1. matches newline解释:
^ # beginning of line
( # group 1
\h* # 0 or more horizontal spaces
) # end group
(?= # positive lookahead, make sure we have after:
def\b # literally "def" and a word boundary, in order to not match "default"
.* # 0 or more any character but newline
(\R) # group 2, any kind of linebreak
) # end lookahead
Run Code Online (Sandbox Code Playgroud)
替代品:
$1 # content of group 1, the spaces to insert
@jit\(parallel=True\) # literally
$2 # content of group 2, the linebreak used in the file
$1 # content of group 1, the spaces to insert
Run Code Online (Sandbox Code Playgroud)
截图(之前):
截图(之后):