title: Powershell Regex for mm-dd-yyyy
body: <p>I am using Powershell to search through a large file to find all strings that contain anything in mm-dd-yyyy format. I then need to extract the string to determine if the date is a valid date. The script works for the most part but is returns too many results and doesn't provide all the info I would like. There are strings in the file like 012-34-5678 and for this I would get a failure on and the value of 12-34-5678 would be returned as an invalid date. I'm also not able to return the line number that the invalid date was found on. Can someone please take a look at my script below and see what I may be doing wrong? </p>
  
  <p>The two commented out lines will return the string number and the entire string that was found on that line, but I do not know how to take just the mm-dd-yyyy part from the line and determine if it is a valid date.</p>
  
  <p>Any help would be greatly appreciatedd. Thanks.</p>
  
  <pre><code>#$matches = Select-String -Pattern $regex -AllMatches -Path "TestFile_2013_01_06.xml" | 
  
  #$matches | Select LineNumber,Line
  
  
  $regex = "\d{2}-\d{2}-\d{4}"     
  
  $matches = Select-String -Pattern $regex -AllMatches -Path "TestFile_2013_01_06.xml" |
     Foreach {$_.Matches | Foreach {$_.Groups[0] | Foreach {$_.Value}}}
  
  foreach ($match in $matches) {
  
      #$date = [datetime]::parseexact($match,"MM-dd-yyyy",$null)  
  
      if (([Boolean]($match -as [DateTime]) -eq $false ) -or ([datetime]::parseexact($match,"MM-dd-yyyy",$null).Year -lt "1800")) {
          write-host "Failed $match"
      }
  }
  </code></pre>