1

I'm trying to find a line that matches the below pattern in a file. I can see the pattern is output as I expect but it doesn't match.

String in file

"5/29/2019 12:01:03 PM - Sys - Logged Successfully"

Variables

$pattern = "Logged Successfully"
$datePattern = "5/29/2019"

Code - Working - Matches ok

$reponse = select-string -Path $path\$file -Pattern $pattern -allmatches -simplematch

Code - Not Working

$reponse = select-string -Path $path\$file -Pattern "$($datePattern).*$($pattern)" -allmatches -simplematch

Maybe im missing something very simple, any help greatly appreciated.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
llanato
  • 2,508
  • 6
  • 37
  • 59
  • 3
    Remove `-SimpleMatch` from your second code sample. That parameter causes `Select-String` to do string comparisons, whereas you want a regular expression match. – Ansgar Wiechers May 29 '19 at 11:44
  • Possible duplicate of [Matches not captured when using -SimpleMatch with Select-String](https://stackoverflow.com/questions/33179734/matches-not-captured-when-using-simplematch-with-select-string) – Wiktor Stribiżew May 29 '19 at 13:23

1 Answers1

2

Remove the -simplematch switch from the code sample that is not working and then it will work. You are disabling a regular expression match while using that switch. See this previous SO answer from Mathias R. Jessen where he explains in more detail.

Jay Adams
  • 2,052
  • 1
  • 12
  • 7