在PowerShell中带有嵌入式双引号的正则表达式模式 [英] Regex pattern with embedded double quotes in PowerShell
本文介绍了在PowerShell中带有嵌入式双引号的正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要使用PowerShell在文档上搜索以下关键字:
Following keyword need to be searched on document using PowerShell:
["Allowed Acquisition Clean-Up Period"
$keyword = ""
Get-Content $SourceFileName | Select-String -Pattern $keyword
我要搜索的字符串中有双引号,所以我在如何在此 $ keyword
中提及.
String which I'm searching for has double quotes in it, so I'm struggling how to mention in this $keyword
.
推荐答案
显然,您不仅有双引号,而且还有一个方括号.方括号是正则表达式中的元字符(用于定义字符类),因此您也需要转义这些字符.
Apparently you not only have double quotes, but also an opening square bracket. Square brackets are meta-characters in regular expressions (for defining character classes), so you need to escape those too.
用单引号定义表达式:
$keyword = '\["Allowed Acquisition Clean-Up Period"'
或使用反引号对嵌套的双引号进行转义:
or escape nested double quotes with backticks:
$keyword = "\[`"Allowed Acquisition Clean-Up Period`""
这篇关于在PowerShell中带有嵌入式双引号的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文