记事本++用括号替换括号
notepad++ replacing parantheses with brackets
我正在将我的代码从 MATLAB 转换为 julia,因此我需要替换用于索引的括号:它们在 MATLAB 中属于 ()
类型,在 julia 中属于 []
类型。函数括号在两者中都是相同的类型,即 ()
.
我认为最快的方法是使用 Notepad++,找到所有括号,然后在需要时用括号替换它们。
无论如何它没有按预期工作。
我不会复制我现在正在转换的所有函数,但是一些部分例如:
x= coord(:,1);
y= coord(:,2);
natG_coord(1,1)= sqrt(1/3);
natG_coord(2,1)= -sqrt(1/3);
natG_coord(3,1)= -sqrt(1/3);
natG_coord(4,1)= sqrt(1/3);
for i=1:4
dNG(1,i)= (1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(2,i)= -(1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(3,i)= -(1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(4,i)= (1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
end
我试着找到 \((.*)\)
并用 []
替换,但它没有得到所有的括号。例如,它在声明 x
和 y
时获取了 sqrt
值,但没有获取 natG_coord
索引。在 for
循环中,它只获取每一行的最后一个表达式,即 (1-etaG(i)^2)
,但外部括号,而不是 etaG
索引(这实际上是我需要替换的)。
我在选择中看不到规律,因此无法提出解决方案。
其他不生气的解决方案通过括号来做这个括号很好!
谢谢大家的帮助。
编辑
@stribizhev:最终结果应该是这样的:
x= coord[:,1]
y= coord[:,2]
natG_coord[1,1]= sqrt(1/3)
natG_coord[2,1]= -sqrt(1/3)
natG_coord[3,1]= -sqrt(1/3)
natG_coord[4,1]= sqrt(1/3)
for i=1:4
dNG[1,i]= (1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[2,i]= -(1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[3,i]= -(1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[4,i]= (1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
end
我一次找到 \((.*)\)
并替换为 []
的结果是:
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1)= sqrt(1/3];
natG_coord[2,1)= -sqrt(1/3];
natG_coord[3,1)= -sqrt(1/3];
natG_coord[4,1)= sqrt(1/3];
for i=1:4
dNG[1,i)= (1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[2,i)= -(1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[3,i)= -(1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[4,i)= (1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
end
我一次找到 \(((?>[^()]|(?R))*)\)
并将所有内容替换为 []
的结果是(我知道你说过几次,如果我这样做,它最终会替换所有匹配的大括号):
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1]= sqrt[1/3];
natG_coord[2,1]= -sqrt[1/3];
natG_coord[3,1]= -sqrt[1/3];
natG_coord[4,1]= sqrt[1/3];
for i=1:4
dNG[1,i]= [1+etaG(i)]/4 + csiG[i]*[1+etaG(i)]/2 - [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[2,i]= -[1+etaG(i)]/4 + csiG[i]*[1+etaG(i)]/2 + [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[3,i]= -[1-etaG(i)]/4 + csiG[i]*[1-etaG(i)]/2 + [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[4,i]= [1-etaG(i)]/4 + csiG[i]*[1-etaG(i)]/2 - [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
end
我发现 \(([^()]*)\)
一次全部替换为 []
是:
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1]= sqrt[1/3];
natG_coord[2,1]= -sqrt[1/3];
natG_coord[3,1]= -sqrt[1/3];
natG_coord[4,1]= sqrt[1/3];
for i=1:4
dNG[1,i]= (1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[2,i]= -(1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[3,i]= -(1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[4,i]= (1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
end
所以最后一个正是我要找的。使用 "find next" 命令后,我可以决定它们是否索引括号并替换它们(例如,避免 sqrt
函数输入)。
非常感谢您的帮助。
由于 \(([^()]*)\)
(用 []
代替)对你有用,这里是解释:
\(([^()]*)\)
匹配项:
\(
- 左圆括号
([^()]*)
- 捕获组 1 匹配 (
和 )
以外的零个或多个字符(使用 [^()]*
)
\)
- 右圆括号
上面的正则表达式将匹配所有最后嵌套级别的括号,其中没有任何括号。
回答 Aaron 关于替换引用字符串中的括号的评论,Notepad++ 支持 Boost 条件替换模式真是太好了。我们可以匹配不需要自己修改替换的,其他匹配的用另一个替换。
(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>\(([^()]*)\))
并替换为(?{o1}$+{o1}:[])
。
请注意,"[^"\]*(?:\.[^"\]*)*"
可以正确有效地匹配带有转义实体的 C 字符串。替换模式意味着替换为带引号的字符串(如果 o1
组匹配)或替换为 [
+第 3 组值+]
(如果其他组匹配)。
如果需要替换外平衡括号,请使用
\(((?>[^()]|(?R))*)\)
并替换为 []
(参见 demo)。如果您需要替换重叠的括号子字符串,您需要多次点击 全部替换。
正则表达式解释:
\( # an outer literal opening round bracket
( # start group 1
(?> # start of atomic group
[^()] # any character other than ( and )
| # OR
(?R) # recursively match the whole pattern
)* # end atomic group and repeat zero or more times
) # end of group 1
\) # match a literal closing round bracket
如果您需要替换那些括号的字符串应该以单词字符开头,请使用
(\w+)(\(((?>[^()]|(?2))*)\))
并替换为[]
。参见 demo
这个正则表达式使用 (?2)
subroutine 只是 重复 第二个捕获组子模式。
现在,避免在引号内匹配这些字符串。假设我们有 var d = "r(string here)"
并且我们不想在这里将 ()
变成 []
。而不是 (\w+)(\(((?>[^()]|(?2))*)\))
(用 []
替换),使用
(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>(\w+)(\(((?>[^()]|(?4))*)\)))
和(?{o1}$+{o1}:[])
作为替换。这将保持 var d = "r(string here)"
字符串完整,并将 var f = a(fg()g)
变成 var f = a[fg()g]
.
我正在将我的代码从 MATLAB 转换为 julia,因此我需要替换用于索引的括号:它们在 MATLAB 中属于 ()
类型,在 julia 中属于 []
类型。函数括号在两者中都是相同的类型,即 ()
.
我认为最快的方法是使用 Notepad++,找到所有括号,然后在需要时用括号替换它们。
无论如何它没有按预期工作。
我不会复制我现在正在转换的所有函数,但是一些部分例如:
x= coord(:,1);
y= coord(:,2);
natG_coord(1,1)= sqrt(1/3);
natG_coord(2,1)= -sqrt(1/3);
natG_coord(3,1)= -sqrt(1/3);
natG_coord(4,1)= sqrt(1/3);
for i=1:4
dNG(1,i)= (1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(2,i)= -(1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(3,i)= -(1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
dNG(4,i)= (1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2)/4;
end
我试着找到 \((.*)\)
并用 []
替换,但它没有得到所有的括号。例如,它在声明 x
和 y
时获取了 sqrt
值,但没有获取 natG_coord
索引。在 for
循环中,它只获取每一行的最后一个表达式,即 (1-etaG(i)^2)
,但外部括号,而不是 etaG
索引(这实际上是我需要替换的)。
我在选择中看不到规律,因此无法提出解决方案。
其他不生气的解决方案通过括号来做这个括号很好!
谢谢大家的帮助。
编辑
@stribizhev:最终结果应该是这样的:
x= coord[:,1]
y= coord[:,2]
natG_coord[1,1]= sqrt(1/3)
natG_coord[2,1]= -sqrt(1/3)
natG_coord[3,1]= -sqrt(1/3)
natG_coord[4,1]= sqrt(1/3)
for i=1:4
dNG[1,i]= (1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[2,i]= -(1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[3,i]= -(1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
dNG[4,i]= (1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4
end
我一次找到 \((.*)\)
并替换为 []
的结果是:
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1)= sqrt(1/3];
natG_coord[2,1)= -sqrt(1/3];
natG_coord[3,1)= -sqrt(1/3];
natG_coord[4,1)= sqrt(1/3];
for i=1:4
dNG[1,i)= (1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[2,i)= -(1+etaG(i))/4 + csiG(i)*(1+etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[3,i)= -(1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 + (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
dNG[4,i)= (1-etaG(i))/4 + csiG(i)*(1-etaG(i))/2 - (1-etaG(i)^2)/4 - 2*csiG(i)*(1-etaG(i)^2]/4;
end
我一次找到 \(((?>[^()]|(?R))*)\)
并将所有内容替换为 []
的结果是(我知道你说过几次,如果我这样做,它最终会替换所有匹配的大括号):
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1]= sqrt[1/3];
natG_coord[2,1]= -sqrt[1/3];
natG_coord[3,1]= -sqrt[1/3];
natG_coord[4,1]= sqrt[1/3];
for i=1:4
dNG[1,i]= [1+etaG(i)]/4 + csiG[i]*[1+etaG(i)]/2 - [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[2,i]= -[1+etaG(i)]/4 + csiG[i]*[1+etaG(i)]/2 + [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[3,i]= -[1-etaG(i)]/4 + csiG[i]*[1-etaG(i)]/2 + [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
dNG[4,i]= [1-etaG(i)]/4 + csiG[i]*[1-etaG(i)]/2 - [1-etaG(i)^2]/4 - 2*csiG[i]*[1-etaG(i)^2]/4;
end
我发现 \(([^()]*)\)
一次全部替换为 []
是:
x= coord[:,1];
y= coord[:,2];
natG_coord[1,1]= sqrt[1/3];
natG_coord[2,1]= -sqrt[1/3];
natG_coord[3,1]= -sqrt[1/3];
natG_coord[4,1]= sqrt[1/3];
for i=1:4
dNG[1,i]= (1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[2,i]= -(1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[3,i]= -(1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 + (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
dNG[4,i]= (1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 - (1-etaG[i]^2)/4 - 2*csiG[i]*(1-etaG[i]^2)/4;
end
所以最后一个正是我要找的。使用 "find next" 命令后,我可以决定它们是否索引括号并替换它们(例如,避免 sqrt
函数输入)。
非常感谢您的帮助。
由于 \(([^()]*)\)
(用 []
代替)对你有用,这里是解释:
\(([^()]*)\)
匹配项:
\(
- 左圆括号([^()]*)
- 捕获组 1 匹配(
和)
以外的零个或多个字符(使用[^()]*
)\)
- 右圆括号
上面的正则表达式将匹配所有最后嵌套级别的括号,其中没有任何括号。
回答 Aaron 关于替换引用字符串中的括号的评论,Notepad++ 支持 Boost 条件替换模式真是太好了。我们可以匹配不需要自己修改替换的,其他匹配的用另一个替换。
(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>\(([^()]*)\))
并替换为(?{o1}$+{o1}:[])
。
请注意,"[^"\]*(?:\.[^"\]*)*"
可以正确有效地匹配带有转义实体的 C 字符串。替换模式意味着替换为带引号的字符串(如果 o1
组匹配)或替换为 [
+第 3 组值+]
(如果其他组匹配)。
如果需要替换外平衡括号,请使用
\(((?>[^()]|(?R))*)\)
并替换为 []
(参见 demo)。如果您需要替换重叠的括号子字符串,您需要多次点击 全部替换。
正则表达式解释:
\( # an outer literal opening round bracket
( # start group 1
(?> # start of atomic group
[^()] # any character other than ( and )
| # OR
(?R) # recursively match the whole pattern
)* # end atomic group and repeat zero or more times
) # end of group 1
\) # match a literal closing round bracket
如果您需要替换那些括号的字符串应该以单词字符开头,请使用
(\w+)(\(((?>[^()]|(?2))*)\))
并替换为[]
。参见 demo
这个正则表达式使用 (?2)
subroutine 只是 重复 第二个捕获组子模式。
现在,避免在引号内匹配这些字符串。假设我们有 var d = "r(string here)"
并且我们不想在这里将 ()
变成 []
。而不是 (\w+)(\(((?>[^()]|(?2))*)\))
(用 []
替换),使用
(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>(\w+)(\(((?>[^()]|(?4))*)\)))
和(?{o1}$+{o1}:[])
作为替换。这将保持 var d = "r(string here)"
字符串完整,并将 var f = a(fg()g)
变成 var f = a[fg()g]
.