Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
Regex 在PowerShell中使用正则表达式_Regex_Powershell - Fatal编程技术网

Regex 在PowerShell中使用正则表达式

Regex 在PowerShell中使用正则表达式,regex,powershell,Regex,Powershell,我有一个包含多个表达式的文件,如$REGX('CareMedic.2_0','CustomerInformation','Customer Information')。该文件可以是xml文件、文本文件或任何其他类型。如果文件包含其中的九个表达式,我将尝试提取所有九个表达式 我的代码编写如下: $input_path = ‘C:\Users\rparpani\Desktop\test2.xml’ $output_file = 'C:\Users\rparpani\Desktop\test2.tex

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

我的代码编写如下:

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

Write-Output($output)

我的正则表达式似乎有问题。有人能建议实现这一点的正确方法吗?

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

$regex = '\$REGX'
您还可以让它自动转义:

$regex = [regex]::Escape('$REGX')

我认为您需要在正则表达式中转义$(\$),否则它会在行尾声明位置。我尝试过这样做,但它不会提取任何值。如果文件中有9个REGX表达式,我希望将它们全部提取出来。有什么建议吗?如果您尽可能地运行代码-allmatches-您会得到什么?当我尝试你的代码时,它对我有效。我只尝试使用txt文件,而没有使用xml@Itchydon它写入我的txt文件,但它只是$RESX。我必须在()之间提取值。一旦这些值被提取出来,我必须将它们分开并存储在3个不同的列中。我尝试过改变,但它似乎没有提取任何值。在你的帖子中的示例中,它显示为“$REGX”,但在你使用“$RESX”的代码中,可能是输入错误?这是一个输入错误。我改正了