-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
  • 856
  • 2
  • 15
  • 33
caliGeek
  • 409
  • 2
  • 7
  • 19
  • I think you need to escape the $ (\$) in your regex otherwise it asserts the position at the end of the line – Itchydon Mar 09 '20 at 17:16
  • I tried doing that but it does not pull any values. If the file has 9 of the REGX expressions, I expect to pull them all. – caliGeek Mar 09 '20 at 17:30
  • @Itchydon Any suggestions? – caliGeek Mar 09 '20 at 18:02
  • If you run the code as far as -allmatches - what do you get? When I try your code it works for me. I have only tried with a txt file not with an xml – Itchydon Mar 09 '20 at 18:20
  • @Itchydon it writes to my txt file but it is just $RESX. I have to pull values between (). Once those values are extracted, I have to strip them apart and store it in 3 different columns – caliGeek Mar 09 '20 at 18:40
  • How about if you run up to -allmatches only - do you get the correct values? – Itchydon Mar 09 '20 at 18:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209333/discussion-between-raj-parpani-and-itchydon). – caliGeek Mar 09 '20 at 18:45
  • I have updated the code on chat – Itchydon Mar 09 '20 at 21:42

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
  • 157,619
  • 12
  • 148
  • 206