-1

I have a file with multiple expressions like $REGX('CareMedic.2_0','CustomerInformation','Customer Information'). The file can be an xml file, text file or any other type. If the file contains nine of those expressions, I'm trying to pull all nine.

I've written my code as below:

$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)

Something seems to be wrong with my regex expression. Can someone please suggest the right way to achieve this?

deralbert
  • 758
  • 2
  • 11
  • 31
Raj Parpani
  • 319
  • 2
  • 6
  • 17

1 Answers1

0

$ is a metacharacter in regex, meaning "end of string", so if you're looking for the literal string $RESX, you need to escape $:

$regex = '\$REGX'

You can also have it escaped automatically:

$regex = [regex]::Escape('$REGX')
Mathias R. Jessen
  • 135,435
  • 9
  • 130
  • 184