Need a regex to find the word "ERROR:", but not match "ERROR: Opening Local File"

this seems to do what you want - it uses the way that PoSh can apply a regex match to an entire collection. [grin]

what it does ...

  • creates a sample data set
    when ready to do this for real, replace the entire #region/#endregion block with a Get-Content call.
  • sets the wanted pattern
  • sets the unwanted pattern
  • uses chained regex calls to exclude the unwanted and then to include the wanted
  • assigns that to the $Result collection
  • displays that on screen

the code ...

#region >>> fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
ERROR: Writing local file  (I want to be alerted to this)
ERROR: Opening local file  (I do not want to be alerted to this)
INFO: Some other thing
ERROR: Yet another thing
INFO: A thing that is wanted
WARNING: Something that may be wanted
'@ -split [System.Environment]::NewLine
#endregion >>> fake reading in a text file

$TargetPattern = 'error:'
$NotWantedPattern = ': opening local file'

$Result = $InStuff -notmatch $NotWantedPattern -match $TargetPattern

$Result

output ...

ERROR: Writing local file  (I want to be alerted to this)
ERROR: Yet another thing

As @spikey_richie says, you need negative lookahead:

ERROR: (?!Opening local file)

Basically, instead of specifying what you're looking for, you specify what you don't want, and wrap that inside (?! and ).

Demo


Something like that should work too : Demo

$MyString = @'
ERROR: Writing local file  (I want to be alerted to this)
ERROR: Opening local file  (I don't want to be alerted to this)
INFO: Some other thing
ERROR: Yet another thing
INFO: A thing that is wanted
WARNING: NOT a not-wanted thing
'@ -split [System.Environment]::NewLine

$pattern = 'ERROR: (?!Opening local file).+'
$MyString | %{ [regex]::matches($_,$pattern) } | %{ $_.Groups[0].Value }