-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?

8
  • I think you need to escape the $ (\$) in your regex otherwise it asserts the position at the end of the line Commented Mar 9, 2020 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. Commented Mar 9, 2020 at 17:30
  • @Itchydon Any suggestions? Commented Mar 9, 2020 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 Commented Mar 9, 2020 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 Commented Mar 9, 2020 at 18:40

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

3 Comments

I tried changing that but it does not appear to pull any values
In the example in your post it says "$REGX", but in the code you use "$RESX", typo perhaps?
It was a typo. I corrected it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.