请求提供PowerShell正则提取动态字段值的实现方案
Problem Statement
I've already converted escaped double quotes (") in my string to regular double quotes, resulting in a formatted JSON snippet. Now I need to extract three dynamic values corresponding to Terminated User Mail, Terminated Account, and Last Day (specifically Test.User@gmail.com, Test User, 2019-06-26 respectively). I'm unsure how to do this, especially with PowerShell regex—could someone help out?
Solution
Hey there! Let's break this down. First off, using regex to parse JSON is generally not recommended because JSON has nested structures and edge cases that regex can easily break on. The far more reliable approach is to use PowerShell's built-in JSON parsing capabilities. But I'll cover both methods for you just in case you need the regex route for some reason.
Method 1: Recommended - Parse JSON Properly
If your formatted JSON looks something like this (example structure matching your values):
{ "Terminated User Mail": "Test.User@gmail.com", "Terminated Account": "Test User", "Last Day": "2019-06-26" }
You can use ConvertFrom-Json to turn it into a PowerShell object, then access the properties directly:
# Assume your formatted JSON string is stored in $jsonString $jsonObject = $jsonString | ConvertFrom-Json # Extract the values $terminatedMail = $jsonObject."Terminated User Mail" $terminatedAccount = $jsonObject."Terminated Account" $lastDay = $jsonObject."Last Day" # Output them to verify Write-Host "Terminated User Mail: $terminatedMail" Write-Host "Terminated Account: $terminatedAccount" Write-Host "Last Day: $lastDay"
This method is robust, handles spaces in property names perfectly, and won't break if the JSON structure changes slightly (like property order shifting).
Method 2: Regex Approach (If You Must)
If you absolutely need to use regex, here's a pattern that targets the specific keys and captures their corresponding values. Note that this assumes your JSON is well-formatted and the keys/values are on single lines (adjust if your JSON is minified):
# Your formatted JSON string $jsonString = @" { "Terminated User Mail": "Test.User@gmail.com", "Terminated Account": "Test User", "Last Day": "2019-06-26" } "@ # Regex patterns for each value $mailPattern = '"Terminated User Mail"\s*:\s*"([^"]+)"' $accountPattern = '"Terminated Account"\s*:\s*"([^"]+)"' $lastDayPattern = '"Last Day"\s*:\s*"([^"]+)"' # Extract using regex matches $mailMatch = [regex]::Match($jsonString, $mailPattern).Groups[1].Value $accountMatch = [regex]::Match($jsonString, $accountPattern).Groups[1].Value $lastDayMatch = [regex]::Match($jsonString, $lastDayPattern).Groups[1].Value # Output results Write-Host "Terminated User Mail: $mailMatch" Write-Host "Terminated Account: $accountMatch" Write-Host "Last Day: $lastDayMatch"
The regex works by:
- Matching the exact key in quotes (
"Terminated User Mail") - Allowing optional whitespace (
\s*) around the colon - Capturing all characters inside the value quotes that aren't a quote (
([^"]+))
Again, I strongly suggest sticking with the JSON parsing method—it's the right tool for the job.
内容的提问来源于stack exchange,提问作者goe

