简体   繁体   English

Powershell - 如何用字符串中的变量替换数字?

[英]Powershell - How to replace a number with a variable in a string?

Trying to replace a number (20 with a variable $cntr=120) in a string using replace operator.尝试使用替换运算符替换字符串中的数字(20 和变量 $cntr=120)。 But getting stuck with $cntr in the output. Where I am doing wrong?但是在 output 中被 $cntr 困住了。我哪里做错了? Any better solutions please.请有更好的解决方案。

Input string输入字符串

myurl.com/search?project=ABC&startAt=**20**&maxResults=100&expand=log

Desired Output string所需的 Output 字符串

myurl.com/search?project=ABC&startAt=**120**&maxResults=100&expand=log

Actual Output string实际 Output 字符串

myurl.com/search?project=ABC&startAt=**$cntr**&maxResults=100&expand=log

Code:代码:

$str='myurl.com/search?project=ABC&startAt=20&maxResults=100&expand=log'
$cntr=120
$str = $str -replace '^(.+&startAt=)(\d+)(&.+)$', '$1$cntr$3'
$str

You need to你需要

  • Use double quotes to be able to use string interpolation使用双引号可以使用字符串插值
  • Use the unambiguous backreference syntax , ${n} , where n is the group ID.使用明确的反向引用语法${n} ,其中n是组 ID。

In this case, you can use在这种情况下,您可以使用

PS C:\Users\admin> $str='myurl.com/search?project=ABC&startAt=20&maxResults=100&expand=log'
PS C:\Users\admin> $cntr=120
PS C:\Users\admin> $str = $str -replace '^(.+&startAt=)(\d+)(&.+)$', "`${1}$cntr`$3"
PS C:\Users\admin> $str
myurl.com/search?project=ABC&startAt=120&maxResults=100&expand=log

See the .NET regex "Substituting a Numbered Group" documentation :请参阅.NET 正则表达式“替换编号组”文档

All digits that follow $ are interpreted as belonging to the number group. $后面的所有数字都被解释为属于数字组。 If this is not your intent, you can substitute a named group instead.如果这不是您的意图,您可以用命名组代替。 For example, you can use the replacement string ${1}1 instead of $11 to define the replacement string as the value of the first captured group along with the number "1".例如,您可以使用替换字符串${1}1而不是$11来将替换字符串定义为第一个捕获组的值以及数字“1”。

A couple things here:这里有几件事:

If you just add the "12" you end up with $112$3 which isn't what you want.如果您只添加“12”,您最终会得到 $112$3,这不是您想要的。 What I did was appended a slash in front and then removed it on the backend, so the replace becomes $1\12$3.我所做的是在前面附加一个斜杠,然后在后端将其删除,因此替换为 $1\12$3。

$str='myurl.com/search?project=ABC&startAt=20&maxResults=100&expand=log'
$cntr=12
$str = ($str -replace '^(.+&startAt=)(\d+)(&.+)$', ('$1\' + $cntr.ToString() +'$3')).Replace("\", "")
$str

Looking to see if there's another way to add the literal "12" in the replace section with the extra character, but this does work.看看是否有另一种方法可以在替换部分中添加带有额外字符的文字“12”,但这确实有效。

Here's another way to do it where you have a literal string between the $1 and $3 and then replace that at the end.这是另一种方法,在 $1 和 $3 之间有一个文字字符串,然后在末尾替换它。

$str='myurl.com/search?project=ABC&startAt=20&maxResults=100&expand=log'
$cntr=12
$str = ($str -replace '^(.+&startAt=)(\d+)(&.+)$', ('$1REPLACECOUNTER$3')).Replace("REPLACECOUNTER", "$cntr")
$str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM