从选择文本中查找并替换字符 NotePad++

从选择文本中查找并替换字符 NotePad++

我的代码中有很多类似于以下内容的文本,我正在尝试使用 notepad++ 进行编辑:

except ValueError, err:
except Exception,err:
except RPCFault, err:

我不想做的是修改如下文本:

except ValueError as err:
except Exception as err:
except RPCFault as err:

为了进行上述更改,我尝试 except.*,在查找框和替换框中使用正则表达式:: except.* as

但这没有帮助。请告诉我该怎么做。

答案1

在搜索模式下正则表达式替换

(except\ [A-Za-z]+),\ *(err:)

$1 as $2

答案2

假设每行只有一个逗号:

  • Ctrl+H
  • 找什么:^except [^,]+\K, *(?=err:$), *(?=err:$)
  • 用。。。来代替:as
  • 检查环绕
  • 检查正则表达式
  • Replace all

解释:

^                   : begining of line
  except [^,]+      : literally "except", followed by a space and 1 or more non comma
  \K                : forget all we have seen until this position
  ,                 : a comma
   *                : 0 or more spaces
  (?=               : look ahead, zero length assertion that makes sure we have after
    err:$           : literally "err:" at end of line
  )                 : end lookahead

替代品:

 as     : a space, "as", a space

给定示例的结果:

except ValueError as err:
except Exception as err:
except RPCFault as err:

相关内容