notepad++ 用括号替换括号 [英] notepad++ replacing parantheses with brackets

查看:58
本文介绍了notepad++ 用括号替换括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在将我的代码从 MATLAB 转换为 julia,因此我需要替换用于索引的括号:它们在 MATLAB 中属于 () 类型,并且属于 [] 在朱莉娅.函数括号在两者中的类型相同,即 ().
我认为最快的方法是使用 Notepad++,找到所有的括号,然后在需要时用括号替换它们.
无论如何,它没有按预期工作.

我不会复制我现在正在转换的所有功能,而是复制一些部分作为示例:

x=坐标(:,1);y= 坐标(:,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);对于 i=1:4dNG(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;结尾

我尝试找到 ((.*)) 并替换为 [$1],但它没有得到所有括号.例如,它在声明 xy 时获得了 sqrt 值,但没有获得 natG_coord索引.在 for 循环中,它只获取每一行的最后一个表达式,即 (1-etaG(i)^2),但是外部括号,而不是 etaG 索引(这实际上是我需要替换的).

我看不到选择的模式,因此无法提出解决方案.
其他不通过括号来做这个括号的解决方案很好!
谢谢大家的帮助.

编辑
@stribizhev:最终结果应该是这样的:

x=坐标[:,1]y= 坐标[:,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)对于 i=1:4dNG[1,i]= (1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 - (1-etaG[i]^​​2)/4 - 2*csiG[i]*(1-etaG[i]^​​2)/4dNG[2,i]= -(1+etaG[i])/4 + csiG[i]*(1+etaG[i])/2 + (1-etaG[i]^​​2)/4 - 2*csiG[i]*(1-etaG[i]^​​2)/4dNG[3,i]= -(1-etaG[i])/4 + csiG[i]*(1-etaG[i])/2 + (1-etaG[i]^​​2)/4 - 2*csiG[i]*(1-etaG[i]^​​2)/4dNG[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结尾

我发现 ((.*)) 并用 [$1] 替换一次是:

x=坐标[:,1];y= 坐标[:,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];对于 i=1:4dNG[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;结尾

我找到 (((?>[^()]|(?R))*)) 并将所有内容替换为 [$1]一次是(我知道你说过几次,如果我这样做,它会替换最后所有匹配的大括号):

x=坐标[:,1];y= 坐标[:,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];对于 i=1:4dNG[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;结尾

我发现 (([^()]*)) 一次用 [$1] 替换所有内容是:

x=坐标[:,1];y= 坐标[:,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];对于 i=1:4dNG[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;结尾

所以最后一个正是我想要的.一旦我使用查找下一个"命令,我可以决定它们是否正在索引括号并替换它们(例如,避免 sqrt 函数输入).
非常感谢您的帮助.

解决方案

由于(([^()]*))(要替换成[$1]) 为你工作,这里是解释:

(([^()]*))

匹配:

  • ( - 一个左圆括号
  • ([^()]*) - 捕获组 1 匹配除 () 以外的零个或多个字符(与 [^()]*)
  • )- 右圆括号

上面的这个正则表达式将匹配所有最后嵌套的级别括号,其中没有任何括号.

回答 Aaron 关于替换引用字符串内的括号的评论,Notepad++ 支持 Boost 条件替换模式真是太好了.我们可以匹配我们不需要修改和替换的self,并使用另一个替换来替换其他匹配.

(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>(([^()]*)))

并替换为 (?{o1}$+{o1}:[$3]).

请注意,"[^"\]*(?:\.[^"\]*)*" 正确有效地匹配带有转义实体的 C 字符串.替换模式意味着用引用的字符串替换(如果 o1 组匹配)或用 [+Group 3 value+] (如果其他组匹配).

如果需要替换外圆括号,请使用

(((?>[^()]|(?R))*))

并替换为 [$1](参见


I am converting a code of mine from MATLAB to julia, thus I need to replace parentheses used for indexing: they are of the type () in MATLAB and of the type [] in julia. Functions parentheses are of the same type in both, i.e. ().
I thought that the fastest way to do this was to use Notepad++, finding all of the parenthes and then replacing them with brackets when need.
Anyhow it does not work as expected.

I won't copy all of the function I am converting now, but some parts as example:

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

I tried finding ((.*)) and replacing with [$1], but it does not get all of the parentheses. For instance, it gets the ones in declaring x and y, the sqrt value but does not get the natG_coord indexes. In the for cycle, it only gets the last expression of each line, i.e. (1-etaG(i)^2), but the external parenthes, not the etaG index (which is actually what I need to replace).

I cannot see a pattern in the choice and thus cannot come up with a solution.
Other solutions not to get mad doing this parenthesis by parenthesis is fine!
Thank you all for your help.

edit
@stribizhev: the final result should be this:

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

What I get finding ((.*)) and replacing with [$1] one time is:

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

What I get finding (((?>[^()]|(?R))*)) and replacing all with [$1] one time is (I know you said several times, if I do it it'll replace every matching braces in the 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

What I get finding (([^()]*)) replacing all with [$1] one time is:

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

So the last one is exactly what I was looking for. Once I go with the "find next" command, I can decide whether they are indexing parantheses or not and substitute them or not (avoiding the sqrt function input, for instance).
Thank you very much for your help.

解决方案

Since the (([^()]*)) (to replace with [$1]) worked for you, here is the explanation:

(([^()]*))

Matches:

  • ( - an opening round bracket
  • ([^()]*) - Capture group 1 matches zero or more characters other than ( and ) (with [^()]*)
  • )- a closing round bracket

This regex above will match all last nested level parentheses, that do not have any parentheses inside them.

Answering Aaron's remark about replacing the parentheses inside the quoted strings, it is great that Notepad++ supports Boost conditional replacement patterns. We can match what we do not need to modify and replace with self, and use another replacement for the other matches.

(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>(([^()]*)))

And replace with (?{o1}$+{o1}:[$3]).

Note that "[^"\]*(?:\.[^"\]*)*" matches C strings with escaped entities correctly and efficiently. The replacement pattern means to replace with the quoted string (if o1 group matched) or with [+Group 3 value+] (if the other group matched).

If you need to replace outer balanced parentheses, use

(((?>[^()]|(?R))*))

And replace with [$1] (see demo). If you need to replace the overlapping parenthetical substrings, you will need to hit Replace All several times.

Regex explanation:

(            # 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

If the strings you need to replace those parentheses should be preceded with word characters, use

(w+)((((?>[^()]|(?2))*)))

And replace with $1[$3]. See demo

This regex uses a (?2) subroutine that just repeats the second capture group subpattern.

Now, avoiding to match these inside quoted strings. Assume we have var d = "r(string here)" and we do not want to turn the () to [] here. Instead of (w+)((((?>[^()]|(?2))*))) (with $1[$3] replacement), use

(?<o1>"[^"\]*(?:\.[^"\]*)*")|(?<o2>(w+)((((?>[^()]|(?4))*))))

And (?{o1}$+{o1}:$3[$5]) as the replacement. This will keep var d = "r(string here)" string intact, and will turn var f = a(fg()g) into var f = a[fg()g].

这篇关于notepad++ 用括号替换括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆