[英]notepad++ regular expression replace an inner sequence of matched text
How would i replace all ocurances of "cast(net_price as decimal(9,2))=xxx AND" with "ROUND(net_price,2)=xxx AND" whilst maintaining xxx in Notepad++?我如何在 Notepad++ 中保持 xxx 的同时用“ROUND(net_price,2)=xxx AND”替换所有出现的“cast(net_price as decimal(9,2))=xxx AND”? The statements are nested with other AND statements as below:这些语句与其他 AND 语句嵌套,如下所示:
What are the required find and replace parameters for notepad++? notepad++ 所需的查找和替换参数是什么?
Example text:示例文本:
OR (cast(net_price as decimal(9,2))=@var2 AND cost=@var3 AND blah)
Example result:示例结果:
OR (ROUND(net_price,2)=@var2 AND cost=@var3 AND blah)
Every time i try to write my own regex it seems to select the entire line from net_price up to the last AND, i want it to select up to the first AND after net_price, then continue on to the next one.每次我尝试编写自己的正则表达式时,似乎 select 从 net_price 到最后一个 AND 的整行,我希望它到 select 到第一个 AND 在 net_price 之后,然后继续下一个。
I had thought that net_price.*AND
would match net_price then anything until it found AND.我原以为net_price.*AND
会匹配 net_price 然后任何东西,直到找到 AND。 Plainly i am wrong and if someone could explain why i would appreciate it.显然我错了,如果有人能解释为什么我会很感激。
cast\((net_price) as decimal\(9,2\)\)(?==@var2 AND)
查找内容: cast\((net_price) as decimal\(9,2\)\)(?==@var2 AND)
ROUND\($1,2\)
替换为: ROUND\($1,2\)
Explanation:解释:
cast\( # literally
(net_price) # group 1, "net_price"
as decimal\(9,2\)\) # literally
(?==@var2 AND) # positive lookahead, make sure we have "=@var2 AND" after
Replacement:替代品:
ROUND\($1,2\)
Screenshot (before):截图(之前):
Screenshot (after):截图(之后):
Apologies for the oversimplified question i have edited it to reflect the true more complicated question.为过于简单的问题道歉,我对其进行了编辑以反映真正更复杂的问题。 Posting the answer in the hopes of giving back.发布答案以期回馈。
I should have pointed out that the sample data can be decimal(9,2) or float, or even decimal(9,4).我应该指出,样本数据可以是小数(9,2)或浮点数,甚至可以是小数(9,4)。 so its much more complicated than just a search and replace.所以它比搜索和替换要复杂得多。 Sorry in trying to simplify for posting i oversimplified!!很抱歉试图简化发布我过于简单了!
It turned out that the correct reg is not.* as this is greedy match, I needed to use the.*?事实证明,正确的 reg 不是。* 因为这是贪婪匹配,我需要使用。*? to match up to only the first AND statement after the net_price.仅匹配 net_price 之后的第一个 AND 语句。 As a greedy match will match to the last pattern it can find and then look backward to match the last AND.由于贪心匹配将匹配它可以找到的最后一个模式,然后向后看以匹配最后一个 AND。 which would select far too much of the line - which is what my problem was.这将 select 太多了 - 这就是我的问题。
So the regex becomes: cast\((net_price).*?\=(.*?) AND
which:所以正则表达式变为: cast\((net_price).*?\=(.*?) AND
其中:
finds cast(
finds net_price and puts it into capture group 1
non greedy scans until it finds an equals character
captures non greedily the value into group 2
finds the AND
which captures into 2 groups net_price always in group 1 and the variable/quantity always in the group 2, everything else gets rewritten:它总是在第 1 组中捕获 2 组 net_price 和始终在第 2 组中的变量/数量,其他所有内容都被重写:
The replace becomes: ROUND\($1,2\)=$2 AND
替换变为: ROUND\($1,2\)=$2 AND
Which replaces $1 (capture group 1) with net_price and $2 (capture group 2) with the value.它将 $1(捕获组 1)替换为 net_price,将 $2(捕获组 2)替换为值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.