[英]Powershell Search String in File Containing $
I want to search the following string in a text file: $$u$$
我想在文本文件中搜索以下字符串: $$u$$
I tried select-string
, get-content
, .Contains
. 我尝试了select-string
, get-content
, .Contains
。 It seems to me it's not possible. 在我看来这是不可能的。
I used this for the search as a variable: $ToSearch = "'$'$u'$'$"
我使用它作为变量进行搜索: $ToSearch = "'$'$u'$'$"
It always gives false result. 它总是给出错误的结果。
It is because most search filters are relying on regex. 这是因为大多数搜索过滤器都依赖于正则表达式。 The $ symbol in regex needs to be escaped 正则表达式中的$符号需要转义
Without knowing more of what you're trying to accomplish I can't give much of an example, but here is one: 在不了解更多您要完成的工作的情况下,我无法举太多例子,但这是一个例子:
'this is a $$u$$ test' -replace "\$",""
The '\\' is what is escaping the character - meaning to translate it literally. '\\'是转义字符的含义-意味着按字面意义进行翻译。
Edit: Per comment 编辑:每个评论
$Val = 'this is a $$u$$ test'
$Val | Select-String "\$+\w\$+" -quiet
-Quiet switch returns t/f rather than a string value. -安静的开关返回t / f而不是字符串值。
The $ is a reserved character in regex, which is complicating your search. $是正则表达式中的保留字符,这会使您的搜索复杂化。
The default search pattern type for Select-String is regex, but you can also specify -Simplematch
or -Wildcard
, either of which will eliminate the need to escape the $. Select-String的默认搜索模式类型是regex,但是您也可以指定-Simplematch
或-Wildcard
,这两种类型都将消除对$进行转义的需要。 If you use -Wildcard, you'll need to include the wilcard * at either end of the match - '*$$u$$*'
. 如果使用-Wildcard,则需要在比赛的任一行末添加通配符*- '*$$u$$*'
。 For simplematch, just use the string you want to match for - '$$u$$'
. 对于simplematch,只需使用要匹配的字符串- '$$u$$'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.