String Powershell-匹配可能包含空格的字符串

String Powershell-匹配可能包含空格的字符串,string,powershell,replace,contains,String,Powershell,Replace,Contains,使用Powershell版本3&读取文件的内容,然后我需要查看文件中是否包含多个字符串中的一个,如果有,则替换它们。在我的例子中,问题是我需要匹配的一个字符串中可能有可变数量的空格(或者根本没有空格) 我匹配的字符串中有双引号,后跟一个冒号(:),然后是空格(或无),然后是任意数量的状态(可以是字母或数字),后跟一个逗号。为了简单起见,我只是在下面的代码中使用了一个数字 $txt = (Get-Content $file) $oldstr = "`"status`": 1," $newstr =

使用Powershell版本3&读取文件的内容,然后我需要查看文件中是否包含多个字符串中的一个,如果有,则替换它们。在我的例子中,问题是我需要匹配的一个字符串中可能有可变数量的空格(或者根本没有空格)

我匹配的字符串中有双引号,后跟一个冒号(:),然后是空格(或无),然后是任意数量的状态(可以是字母或数字),后跟一个逗号。为了简单起见,我只是在下面的代码中使用了一个数字

$txt = (Get-Content $file)
$oldstr = "`"status`": 1,"
$newstr = '`"status`": 0,"
if (($txt.Contains($old1)) -or ($txt.Contains($oldstr)) -or ($txt.Contains($old2))) {
    $txt.Replace($oldstr, $newstr).Replace($old1, $new1).replace($old2, $new2)| Set-Content -Path $file
}
我遇到的问题是匹配
$oldstr
,它可能在冒号和状态码之间没有空格,在本例中,状态码是一个数字,但也可能是几个不同的数字或字符串。
$newstr
无需从
$oldstr
复制空白。此外,在上面的示例中,它正在使用Contains中的三个条件之一。实际数据可能不包含、一个、两个或全部三个字符串


如何匹配/包含和替换可能包含空格的字符串

使用带有
-replace
运算符的正则表达式:

PS C:\> '"status":      0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status": 0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status":0' -replace '"status":\s*0','"status": 1'
"status": 1
在我上面使用的模式中:

  • “状态”:
    仅与文本字符串
    “状态”:
  • \s*
    匹配0个或更多空白字符
  • 0
    匹配文本
    0

使用带有
-replace
运算符的正则表达式:

PS C:\> '"status":      0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status": 0' -replace '"status":\s*0','"status": 1'
"status": 1
PS C:\> '"status":0' -replace '"status":\s*0','"status": 1'
"status": 1
在我上面使用的模式中:

  • “状态”:
    仅与文本字符串
    “状态”:
  • \s*
    匹配0个或更多空白字符
  • 0
    匹配文本
    0
使用转换为组合正则表达式的哈希表来匹配/替换多个对。但是我没有在散列键中输入正则表达式,所以我对foreach中的$执行了表和正则表达式

# Build hashtable of search and replace values.

$file = ".\testfile.txt"

$replacements = @{
  'something2' = 'somethingelse2'
  'something3' = 'somethingelse3'
  'morethings' = 'morethingelses'
  'blabla'    = 'blubbblubb'
}
# Join all keys from the hashtable into one regular expression.
[regex]$r = @($replacements.Keys | foreach { [regex]::Escape( $_ ) }) -join '|'

[scriptblock]$matchEval = { param( [Text.RegularExpressions.Match]$matchInfo )
  # Return replacement value for each matched value.
  $matchedValue = $matchInfo.Groups[0].Value
  $replacements[$matchedValue]
}
$fileCont = Get-Content $file
# Perform replace over every line in the file and append to log.
$Newfile = $fileCont | ForEach { 
  $r.Replace( ( $_ -replace '"status":\s*0','"status": 1'), $matchEval ) 
} 

$fileCont
"----"
$Newfile
以my testfile.txt的形式给出此输出

> .\Replace-Array.ps1
"Status":  0, something2,morethings
"Status":    0, something3, blabla
----
"status": 1, somethingelse2,morethingelses
"status": 1, somethingelse3, blubbblubb
使用转换为组合正则表达式的哈希表来进行多个匹配/替换对。但是我没有在散列键中输入正则表达式,所以我对foreach中的$执行了表和正则表达式

# Build hashtable of search and replace values.

$file = ".\testfile.txt"

$replacements = @{
  'something2' = 'somethingelse2'
  'something3' = 'somethingelse3'
  'morethings' = 'morethingelses'
  'blabla'    = 'blubbblubb'
}
# Join all keys from the hashtable into one regular expression.
[regex]$r = @($replacements.Keys | foreach { [regex]::Escape( $_ ) }) -join '|'

[scriptblock]$matchEval = { param( [Text.RegularExpressions.Match]$matchInfo )
  # Return replacement value for each matched value.
  $matchedValue = $matchInfo.Groups[0].Value
  $replacements[$matchedValue]
}
$fileCont = Get-Content $file
# Perform replace over every line in the file and append to log.
$Newfile = $fileCont | ForEach { 
  $r.Replace( ( $_ -replace '"status":\s*0','"status": 1'), $matchEval ) 
} 

$fileCont
"----"
$Newfile
以my testfile.txt的形式给出此输出

> .\Replace-Array.ps1
"Status":  0, something2,morethings
"Status":    0, something3, blabla
----
"status": 1, somethingelse2,morethingelses
"status": 1, somethingelse3, blubbblubb

好吧,你需要正则表达式来完成;)但我帮不了你;)好吧,你需要正则表达式来完成;)但我帮不了你;)