0

In my PowerShell script I am getting an error that I dont understand.

The error is:

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

Invalid regular expression pattern:
Menu "User" {
   Button "EXDS" {
      Walk_Right "EXDS"
   }
}
.
At C:\test.ps1:7 char:18
+ ($output -replace <<<<  $target) | Set-Content "usermenuTest2.4d.new"
    + CategoryInfo          : InvalidOperation: (
Menu "User" {...do"
  }
}
:String) [], RuntimeException
    + FullyQualifiedErrorId : InvalidRegularExpression

My script reads a file into a string (string A) then attempts to remove String A from another file. What does this error mean and how I can I fix it?

My code:

#set-executionpolicy Unrestricted -Force 
#set-executionpolicy -scope LocalMachine -executionPolicy Unrestricted -force

$target=[IO.File]::ReadAllText(".\usermenuTest1.4d")
$output=[IO.File]::ReadAllText(".\usermenuTest2.4d")

($output -replace $target) | Set-Content "usermenuTest2.4d.new"

2 Answers 2

2

Try:

($output -replace [regex]::escape($target))

in the -replace $target is always evaluated as a regular expression. In you case $target contains some regex special character and can't be parsed correctly then you need to escape all special characters. The [regex]::escape() .net method helps doing this job.

Sign up to request clarification or add additional context in comments.

Comments

0

That's probably because $target is null (and so is $output).

.NET replaces the dot with the initial working directory in which PowerShell was started (usually either your home directory or systemroot). I'm guessing that usermenuTest1.4d is in a different directory, and you're running this script from that directory. ReadAllText is looking for the file in the initial directory and not finding it.

If you run $target=[IO.File]::ReadAllText(".\usermenuTest1.4d") at the command prompt in the directory in which usermenuTest1.4d is located, you'll see an error telling you that it couldn't find the file and showing you the full path it was looking for, which will be different than what you expected. Or, you could add the following line to your script to see which directory it will replace the dot with:

[environment]::currentdirectory

Any of the following should work:

$target = Get-Content .\usermenuTest1.4d | Out-String

$target = [IO.File]::ReadAllText("$pwd\usermenuTest1.4d")

$target = [IO.File]::ReadAllText((Resolve-Path usermenuTest1.4d))

[environment]::currentdirectory = $pwd
$target=[IO.File]::ReadAllText('.\usermenuTest1.4d')

The last one is unnecessarily cumbersome, but I put it in to help make it clear what's going on.

Of course, you should do the same when setting $output.

1 Comment

If $target or $output are $null no InvalidRegularExpression exception will be trhow. The error in not this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.