在Powrshell中使用正则表达式

问题描述 投票:0回答:1

我有一个带有多个表达式的文件,例如“ $ REGX('CareMedic.2_0','CustomerInformation','Customer Information')”。该文件可以是xml文件,文本文件或任何其他类型。如果文件包含这些表达式中的9个,则我尝试提取所有9个。

我编写了如下代码:

$input_path = ‘C:\Users\rparpani\Desktop\test2.xml’
$output_file = 'C:\Users\rparpani\Desktop\test2.text'
$regex = '$REGX'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

我的正则表达式似乎出了点问题。有人可以建议解决此问题的正确方法吗?

regex powershell regular-language
1个回答
0
投票

[$是正则表达式中的元字符,表示“字符串结尾”,因此,如果要查找文字字符串$RESX,则需要转义$

$regex = '\$RESX'

您也可以自动将其转义:

$regex = [regex]::Escape('$RESX')
© www.soinside.com 2019 - 2024. All rights reserved.