Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell中嵌入双引号的正则表达式模式_Powershell_Escaping_Quoting - Fatal编程技术网

PowerShell中嵌入双引号的正则表达式模式

PowerShell中嵌入双引号的正则表达式模式,powershell,escaping,quoting,Powershell,Escaping,Quoting,需要使用PowerShell在文档上搜索以下关键字: ["Allowed Acquisition Clean-Up Period" 我正在搜索的字符串中有双引号,所以我很难在这个$keyword中提到,显然,您不仅有双引号,还有一个方括号。方括号是正则表达式中的元字符(用于定义字符类),所以您也需要对其进行转义 用单引号定义表达式: $keyword = '\["Allowed Acquisition Clean-Up Period"' 或使用反勾号转义嵌套双引号: $keyword = "\

需要使用PowerShell在文档上搜索以下关键字:

["Allowed Acquisition Clean-Up Period"
我正在搜索的字符串中有双引号,所以我很难在这个
$keyword

中提到,显然,您不仅有双引号,还有一个方括号。方括号是正则表达式中的元字符(用于定义字符类),所以您也需要对其进行转义

用单引号定义表达式:

$keyword = '\["Allowed Acquisition Clean-Up Period"'
或使用反勾号转义嵌套双引号:

$keyword = "\[`"Allowed Acquisition Clean-Up Period`""
要补充,其中包含正确的解决方案:

鉴于
不是正则表达式元字符(在正则表达式中没有特殊含义),您的问题归结为:
如何在PowerShell中的字符串中嵌入
(双引号)?

  • 旁白:如上所述,
    [
    是一个regex元字符,因此它必须在regex中作为
    \[
    转义才能被视为一个文本。正如所指出的,您可以让自己处理转义;结果在regex的上下文中逐字处理
    $string
    的内容
有几个选项,这里用一个简化的示例字符串,
3“of rain
-另请参见:

为完整起见:您可以通过Unicode码点(标识每个字符的数字)创建双引号,即
0x22
(十六进制)/
34
(十进制),将其强制转换为
[char]
,例如:

您可以使用以下选项:

  • 字符串连接中
    '3'+[char]0x22+'ofrain'
  • 字符串格式表达式中,使用
    -f
    运算符
    '3{0}of rain'-f[char]0x22

只是一个提示:如果您不确定将来要转义什么,可以使用
[regex]::Escape()
方法。上述方法不适用于LIKE子句$SearchKeyword1='[“Lookup keyword”'if($para.Range.Text-LIKE$SearchKeyword1)@user3657339。正则表达式匹配(
-match
Select String
)和通配符匹配(
-like
)不相同,并且不使用相同的模式。
$keyword = "\[`"Allowed Acquisition Clean-Up Period`""
# Inside a *literal string* ('...'):
# The content of single-quoted strings is treated *literally*.
# Double quotes can be embedded as-is.
'3 " of rain'

# Inside an *expandable string* ("..."):
# Such double-quoted strings are subject to *expansion* (interpolation)
# of embedded variable references ("$var") and expressions ("$(Get-Date)")
# Use `" inside double quotes; ` is PowerShell's escape character.
"3 `" of rain"                                                                                 #"
# Inside "...", "" works too.
"3 "" of rain"

# Inside a *literal here-string* (multiline; end delimiter MUST be 
# at the very beginning of a line):
# " can be embedded as-is.
@'
3 " of rain
'@

# Inside an *expanding here-string*:
# " can be embedded as-is, too.
@"
3 " of rain
"@