[英]Regex search & replace versatile lookbehind
I have an R function which is similar to this: 我有一个R函数类似于:
logFun <- function(msg1, msg2){
return(sprintf("%s: %s", msg1, msg2))
}
logFun("123", "456")
It's used in a lot of places, and not always entered nicely eg these are all example use cases: 它在很多地方都使用过,并且输入的内容并不总是很好,例如,这些都是示例用例:
var1 <- "aa"
var2 <- "bb"
logFun(var1, var2)
logFun("aa", var2)
logFun("aa", "bb")
logFun(var1, "bb")
logFun(msg1 = "aa", msg2 = "bb")
logFun(msg1 = var1, msg2 = "bb")
...
Say I had a new function eg 说我有一个新功能,例如
logFun2 <- function(msg1, msg2, type){
return(sprintf("[%s] %s: %s", type, msg1, msg2))
}
logFun2("123", "456", "bug")
and the function calls have to be preserved (msg1, msg2, type)
, as many have been converted from logFun
to logFun2
. 并且必须保留函数调用(msg1, msg2, type)
,因为许多logFun
已从logFun
转换为logFun2
。 What I am trying to do with Sublime is to replace the list of use cases above with the following: 我要对Sublime进行的操作是将上述用例列表替换为以下内容:
logFun2(var1, var2, type = "bug")
logFun2("aa", var2, type = "bug")
logFun2("aa", "bb", type = "bug")
logFun2(var1, "bb", type = "bug")
logFun2(msg1 = "aa", msg2 = "bb", type = "bug")
logFun2(msg1 = var1, msg2 = "bb", type = "bug")
I've read some bits around lookbehind matching, but finding it tricky to find out if its a doable job, or whether I should find another solution to the problem. 我已经阅读了一些后向匹配的内容,但是很难找出它是否可行,或者是否应该找到其他解决方案。 With https://regex101.com/ , I've managed to isolate the logFun(
part with (?<=logFun\\()
, but not sure where to go from here. Any guidance would be appreciated :) 使用https://regex101.com/ ,我设法用(?<=logFun\\()
隔离了logFun(
部分,但不确定从何处去。任何指导都将不胜感激:)
Thanks, Jonny 谢谢乔尼
Edit 1 编辑1
Q: Why cannot you define your logFun2
function with default value for argument type = "bug"
and just replace logFun
with logFun2
? 问:为什么不能用参数type = "bug"
默认值定义logFun2
函数,而只用logFun
替换logFun2
?
A: In reality, we don't just have logFun
. 答:实际上,我们不仅仅拥有logFun
。 We say have functions logBug
, logInfo
, logWarning
etc. We are then changing these in to a singular function logGeneral
eg logBug
becomes logGeneral(.., .., type = "bug")
. 我们说有函数logBug
, logInfo
, logWarning
等。然后将它们更改为单个函数logGeneral
例如logBug
变为logGeneral(.., .., type = "bug")
。 So, yep you are right we could for one type of log specify the default and not worry about that one. 因此,是的,我们可以为一种类型的日志指定默认值,而不用担心该类型。
I don't use Sublime but the regex to isolate your function could be: 我不使用Sublime,但是使用正则表达式来隔离您的功能可能是:
(\blogFun)(\b\(.*)\)
And replace : 并替换:
$12$2, type = "bug")
Since there are multiple types, then one could use a regex for all of them: 由于存在多种类型,因此可以对所有类型使用正则表达式:
\blog(\w+)\(([^()]+)\)
And then replace by using the PCRE lowercase transformation \\L for the type name after "log". 然后使用PCRE小写转换\\ L替换“ log”之后的类型名称。
Replace string: 替换字符串:
logFun2(\2, type = "\L\1")
Using sublime I used for your cases: 使用我用于您的案例的崇高:
Find: (logFun)(\\(.+)\\)
查找: (logFun)(\\(.+)\\)
Replace: \\12\\2, type = "bug")
替换: \\12\\2, type = "bug")
If you have also logBug, logInfo, logWarning
(or even logDebug(paste("annoying", "case"), msg2 = "data_prep")
) then you could modify just the find part: 如果您还有logBug, logInfo, logWarning
(甚至logDebug(paste("annoying", "case"), msg2 = "data_prep")
)),则可以只修改查找部分:
Find: (log[AZ][az]*)(\\(.+)\\)
查找: (log[AZ][az]*)(\\(.+)\\)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.