1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| $csvLine = '张三,"工程部,高级工程师",北京,100001' $fields = [regex]::Matches($csvLine, '(?<=^|,)(?:"(?<q>[^"]*)"|(?<n>[^,]*))')
$values = foreach ($f in $fields) { if ($f.Groups['q'].Success) { $f.Groups['q'].Value } else { $f.Groups['n'].Value } } Write-Host "CSV 字段:$($values -join ' | ')"
$messy = ' Hello World this is a test ' $clean = $messy -replace '\s+', ' ' -replace '^\s+|\s+$', '' Write-Host "清理前:[$messy]" Write-Host "清理后:[$clean]"
$unsafeNames = @( 'Report: Q3/2025 <Final>.xlsx' 'Notes (draft #2).txt' 'Data | Backup & Archive.csv' '配置文件 - 生产环境.json' )
$safeNames = $unsafeNames | ForEach-Object { $safe = $_ -replace '[\\/:*?"<>|]', '_' $safe = $safe -replace '\s+', ' ' $safe = $safe.Trim() [PSCustomObject]@{ Original = $_ Safe = $safe } } $safeNames | Format-Table -AutoSize
function Invoke-RegexRename { param( [string]$Path = ".", [string]$Pattern, [string]$Replace, [switch]$WhatIf )
$files = Get-ChildItem $Path -File $renamed = 0
foreach ($file in $files) { $newName = $file.Name -replace $Pattern, $Replace if ($newName -ne $file.Name) { Write-Host "$($file.Name) -> $newName" if (-not $WhatIf) { Rename-Item $file.FullName -NewName $newName } $renamed++ } }
Write-Host "`n共处理 $renamed 个文件" -ForegroundColor Green }
function Select-LogByTimeRange { param( [string]$LogPath, [datetime]$Start, [datetime]$End, [string]$TimePattern = '(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})' )
$regex = [regex]::new($TimePattern, 'Compiled') $lines = Get-Content $LogPath
$filtered = foreach ($line in $lines) { $match = $regex.Match($line) if ($match.Success) { $timestamp = [datetime]::ParseExact($match.Groups[1].Value, 'yyyy-MM-dd HH:mm:ss', $null) if ($timestamp -ge $Start -and $timestamp -le $End) { $line } } }
Write-Host "时间范围 $Start ~ $End:共 $($filtered.Count) 条" -ForegroundColor Cyan return $filtered }
|