繁体   English   中英

适用于mm-dd-yyyy的Powershell正则表达式

[英]Powershell Regex for mm-dd-yyyy

我正在使用Powershell来搜索一个大文件,以查找包含mm-dd-yyyy格式的所有内容的所有字符串。 然后,我需要提取字符串以确定日期是否为有效日期。 该脚本在大多数情况下都有效,但是返回的结果太多,并且没有提供我想要的所有信息。 文件中有字符串,如012-34-5678,为此,我会失败,并且将12-34-5678的值作为无效日期返回。 我也无法返回发现无效日期的行号。 有人可以看看下面的脚本,看看我做错了什么吗?

两行注释掉的行将返回字符串号和在该行上找到的整个字符串,但是我不知道如何仅从行中获取mm-dd-yyyy部分并确定其是否为有效日期。

任何帮助将不胜感激。 谢谢。

#$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"
    }
}

通过使其更加健壮,您可以在正则表达式本身中进行很多验证:

$regex = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}"

上面的代码匹配从01/01/1900到12/31/2099之间的任何日期,并接受正斜杠,破折号,空格和点作为日期分隔符。 并不像拒绝2月30日或31日,11月31日等日期无效

行号在Select-String输出的对象上可用,但是您没有在$ matches中捕获它。 尝试这个:

$matchInfos = @(Select-String -Pattern $regex -AllMatches -Path "TestFile_2013_01_06.xml")
foreach ($minfo in $matchInfos)
{
    #"LineNumber $($minfo.LineNumber)"
    foreach ($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
    {
        if ($match -isnot [DateTime]) -or 
            ([datetime]::parseexact($match,"MM-dd-yyyy",$null).Year -lt "1800")) {
          Write-host "Failed $match on line $($minfo.LineNumber)"
        }
    }
 }

我可能只会尝试链接Select-String和实际匹配项的结果。 我没有包括检查日期是否足够“新”的条件:

Select-String -Pattern '\d{2}-\d{2}-\d{4}' -Path TestFile_2013_01_06.xml -AllMatches | 
    ForEach-Object {
        $Info = $_ | 
            Add-Member -MemberType NoteProperty -Name Date -Value $null -PassThru |
            Add-Member -MemberType NoteProperty -Name Captured -Value $null -PassThru
        foreach ($Match in $_.Matches) {
            try {
                $Date = [DateTime]::ParseExact($Match.Value,'MM-dd-yyyy',$null)
            } catch {
                $Date = 'NotValid'
            } finally {
                $Info.Date = $Date
                $Info.Captured = $Match.Value
                $Info
            }
        }
    } | Select Line, LineNumber, Date, Captured

当我在一些样本数据上尝试时,我得到了这样的信息:

Line                                  LineNumber Date                Captured  
----                                  ---------- ----                --------  
Test 12-12-2012                                1 2012-12-12 00:00:00 12-12-2012
Test another 12-40-2030                        2 NotValid            12-40-2030
20-20-2020 And yet another 01-01-1999          3 NotValid            20-20-2020
20-20-2020 And yet another 01-01-1999          3 1999-01-01 00:00:00 01-01-1999

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM