In my PowerShell script, I'm running Select-String over a number of files, looking for a string passed into it via a variable ($id):
在我的PowerShell脚本中,我在许多文件上运行selectstring,寻找通过变量($id)传入它的字符串:
foreach ($file in (ls "path\to\files")) {
$found = $false
$found = Select-String -Path $file $id -Quiet
if ($found) {
break
}
}
Unfortunately, the $id variable sometimes things like "\C" or "\T", which Select-String tries to interpret as escape sequences. These are not valid escape sequences, so Select-String throws an error. They are not intended to be escape sequences (e.g., it could be part of a file path such as "C:\Test"), so how can I prevent PowerShell/Select-String from interpreting them as such? I've tried using parentheses around $id with no luck.
不幸的是,$id变量有时像“\C”或“\T”,选择字符串试图将其解释为转义序列。这些不是有效的转义序列,因此Select-String抛出一个错误。它们不打算是转义序列(例如,它可能是文件路径的一部分,如“C:\测试”),那么如何防止PowerShell/Select-String将它们解释为这样呢?我试过用圆括号来表示不走运。
3 个解决方案
#1
32
Use the static escape()
method, it instructs the regular expression engine to interpret these characters literally rather than as metacharacters:
使用静态escape()方法,它指示正则表达式引擎逐字解释这些字符,而不是作为元字符:
$id = [regex]::escape($id)
You can also turn the command to a one liner (-path
can take a collection of files):
您还可以将命令转换为一行(-path可以获取一组文件):
Select-String -Path path\to\files\\* -Pattern ([regex]::escape($id)) -Quiet
#2
0
If the $id string already contains something like TAB when it's passed to you then I'm not aware of a built in method to safely escape it back to "\t". You need to make sure your script is passed the correct string in the first place. I.e. it needs to passed 0x5C74 (\t) not 0x09 (TAB). So the escaping needs to be done when the the search string is first defined.
如果$id字符串在传递给您时已经包含类似TAB的内容,那么我不知道有一个内置的方法可以安全地将其转义回“\t”。首先,您需要确保您的脚本被传递了正确的字符串。例如,它需要传递0x5C74 (\t)而不是0x09 (TAB)。因此,当搜索字符串首次定义时,需要进行转义。
Regex.Escape will escape TAB -> \t but will also escape any of these characters that have meaning within regular expressions:
正则表达式。Escape将会逃脱TAB -> \t,但也会逃脱任何在正则表达式中有意义的字符:
\, *, +, ?, |, {, [, (,), ^, $,., #, and white space
|,\ *,+,{,,^(,),美元。, #,空格
e.g. . -> \.
例如。- > \。
#3
0
Select-String has a -SimpleMatch
parameter that will cause the cmdlet to do simple string matches instead of regular expressions. If you change the script to do:
Select-String有一个-SimpleMatch参数,它将使cmdlet进行简单的字符串匹配,而不是正则表达式。如果你改变剧本去做:
$found = Select-String -Path $file $id -Quiet -SimpleMatch
it should work as desired.
它应该按照需要工作。
#1
32
Use the static escape()
method, it instructs the regular expression engine to interpret these characters literally rather than as metacharacters:
使用静态escape()方法,它指示正则表达式引擎逐字解释这些字符,而不是作为元字符:
$id = [regex]::escape($id)
You can also turn the command to a one liner (-path
can take a collection of files):
您还可以将命令转换为一行(-path可以获取一组文件):
Select-String -Path path\to\files\\* -Pattern ([regex]::escape($id)) -Quiet
#2
0
If the $id string already contains something like TAB when it's passed to you then I'm not aware of a built in method to safely escape it back to "\t". You need to make sure your script is passed the correct string in the first place. I.e. it needs to passed 0x5C74 (\t) not 0x09 (TAB). So the escaping needs to be done when the the search string is first defined.
如果$id字符串在传递给您时已经包含类似TAB的内容,那么我不知道有一个内置的方法可以安全地将其转义回“\t”。首先,您需要确保您的脚本被传递了正确的字符串。例如,它需要传递0x5C74 (\t)而不是0x09 (TAB)。因此,当搜索字符串首次定义时,需要进行转义。
Regex.Escape will escape TAB -> \t but will also escape any of these characters that have meaning within regular expressions:
正则表达式。Escape将会逃脱TAB -> \t,但也会逃脱任何在正则表达式中有意义的字符:
\, *, +, ?, |, {, [, (,), ^, $,., #, and white space
|,\ *,+,{,,^(,),美元。, #,空格
e.g. . -> \.
例如。- > \。
#3
0
Select-String has a -SimpleMatch
parameter that will cause the cmdlet to do simple string matches instead of regular expressions. If you change the script to do:
Select-String有一个-SimpleMatch参数,它将使cmdlet进行简单的字符串匹配,而不是正则表达式。如果你改变剧本去做:
$found = Select-String -Path $file $id -Quiet -SimpleMatch
it should work as desired.
它应该按照需要工作。