79688158

Date: 2025-07-02 22:53:25
Score: 1.5
Natty:
Report link

Here is a pretty simple regex pattern generator. My approach is really simple, just parsing the end user suitable input string like yyyy-MM-dd,HH:mm:ss or 2025-06-05,08:37:38 and building a new regex pattern by exchanging all chars or digits by \d or escaping some chars like ., / or \.

The main issue was to correctly handle the specific [A|P]M pattern, but I think it should be OK. Honestly, it is not super perfect, but fine for getting a clue how it could be done.

Please let me know if you need further explanations about my code and I will add it here tomorrow.

function new-regex-pattern {
    param (
        [string]$s
    )
    $ampm = '[A|P]M'
    if (($s -match [Regex]::Escape($ampm)) -or ($s -match $ampm)) {
        $regexOptions = [Text.RegularExpressions.RegexOptions]'IgnoreCase, CultureInvariant'
        if ($s -match [Regex]::Escape($ampm)) {
            $pattern = -join ('(?<start>.*)(?<AM_PM>', 
              [Regex]::Escape($ampm), ')(?<end>.*)')
        }
        else {
            $pattern = -join ('(?<start>.*)(?<AM_PM>', $ampm, ')(?<end>.*)')
        }
        $regexPattern = [Regex]::new($pattern, $regexOptions)
        $match = $regexPattern.Matches($s)
        return (convert-pattern $match[0].Groups['start'].Value) + 
          $match[0].Groups['AM_PM'].Value + 
          (convert-pattern $match[0].Groups['end'].Value)
    }
    return convert-pattern $s
}

function convert-pattern {
    param (
        [string]$s
    )
    if ($s.Length -gt 0) {
        foreach ($c in [char[]]$s) {
            switch ($c) {
                { $_ -match '[A-Z0-9]' } { $result += '\d' }
                { $_ -match '\s' } { $result += '\s' }
                { $_ -eq '.' } { $result += '\.' }
                { $_ -eq '/' } { $result += '\/' }
                { $_ -eq '\' } { $result += '\\' }
                default { $result += $_ }
            }
        }
    }
    return $result
}

$formatinput1 = 'M/d/yyyy,HH:mm:ss.fff'
$formatinput2 = 'yyyy-MM-dd,HH:mm:ss'
$formatinput3 = 'yyyy-M-d h:mm:ss [A|P]M'

$sampleinput1 = '6/5/2025,08:37:38.058'
$sampleinput2 = '2025-06-05,08:37:38'
$sampleinput3 = '2025-6-5 8:37:38 AM'

$example1 = '6/5/2025,08:37:38.058,1.0527,-39.5013,38.072,1.0527,-39.5013'
$example2 = '2025-06-05,08:37:38,1.0527,-39.5013,38.072,1.0527,-39.5013'
$example3 = '2025-6-5 8:37:38 AM,1.0527,-39.5013,38.072,1.0527,-39.5013'

$regexPattern = [Regex]::new((new-regex-pattern $formatinput1))
Write-Host $regexPattern.Matches($example1)

$regexPattern = [Regex]::new((new-regex-pattern $formatinput2))
Write-Host $regexPattern.Matches($example2)

$regexPattern = [Regex]::new((new-regex-pattern $formatinput3))
Write-Host $regexPattern.Matches($example3)

$regexPattern = [Regex]::new((new-regex-pattern $sampleinput1))
Write-Host $regexPattern.Matches($example1)

$regexPattern = [Regex]::new((new-regex-pattern $sampleinput2))
Write-Host $regexPattern.Matches($example2)

$regexPattern = [Regex]::new((new-regex-pattern $sampleinput3))
Write-Host $regexPattern.Matches($example3)
Reasons:
  • RegEx Blacklisted phrase (2.5): Please let me know
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: burnie