【发布时间】:2021-11-12 05:06:29
【问题描述】:
我有一个关于正则表达式的问题。我想知道是否可以在powershell中对正则表达式的匹配结果评估数学表达式?我不能使用 powershell 来评估它,只需要正则表达式。
问题陈述:
我有如下代码:
$id = $reader.GetValue(0).ToString();
$col1 = $reader.GetValue(1).ToString();
$col2 = $reader.GetValue(2).ToString();
$col3 = $reader.GetValue(3).ToString();
$col4 = $reader.GetValue(4).ToString();
$col5 = $reader.GetValue(5).ToString();
$col6 = $reader.GetValue(6).ToString();
$col7 = $reader.GetValue(7).ToString();
...
我需要使用 Powershell ISE 的查找和替换对话框在 GetValue() 文本中增加 3 个索引 0、1、2 等。
结果应该是这样的:
$id = $reader.GetValue(3).ToString();
$col1 = $reader.GetValue(4).ToString();
$col2 = $reader.GetValue(5).ToString();
$col3 = $reader.GetValue(6).ToString();
$col4 = $reader.GetValue(7).ToString();
$col5 = $reader.GetValue(8).ToString();
$col6 = $reader.GetValue(9).ToString();
$col7 = $reader.GetValue(10).ToString();
...
我试过
Find what: GetValue\((\d)\)
Replace with: GetValue($1+3)
但我没能成功,我找不到任何文件或关于该问题的合理解决方案。
非常感谢您提供任何可能的解决方案。
【问题讨论】:
-
重构你的代码,在左边使用一个数组和一个循环,你不需要做花哨的正则表达式来改变起始索引
-
关于解析数学中缀表示法,请参阅前面的q&a。
-
下面的答案有帮助吗?如果您需要更多帮助,请告知。
-
不幸的是,这不能满足我的问题,因为我想用正则表达式来做。实际上,我正在寻找“仅”正则表达式(在“替换为”部分)是否可能的答案。如果有什么办法。我也找不到解决办法。
-
您使用的是哪个代码编辑器?
标签: regex powershell