0
0
PowerShellscripting~10 mins

Named captures in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Named captures
Input string
Apply regex with named groups
Match found?
NoExit: No match
Yes
Extract named groups
Use named groups in script
The script applies a regex with named groups to a string, checks for a match, then extracts and uses the named groups.
Execution Sample
PowerShell
$text = 'Name: John, Age: 30'
if ($text -match 'Name: (?<name>\w+), Age: (?<age>\d+)') {
  "Name is $($matches['name']) and Age is $($matches['age'])"
}
This script extracts 'name' and 'age' from a string using named captures and prints them.
Execution Table
StepActionEvaluationResult
1Assign string to $text$text = 'Name: John, Age: 30'Variable $text holds 'Name: John, Age: 30'
2Apply regex with named groups to $text$text -match 'Name: (?<name>\w+), Age: (?<age>\d+)'Match found: True
3Extract named group 'name'$matches['name']'John'
4Extract named group 'age'$matches['age']'30'
5Output formatted string"Name is John and Age is 30"Printed: Name is John and Age is 30
💡 Regex match succeeds, named groups extracted, script outputs the formatted string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
$textundefined'Name: John, Age: 30''Name: John, Age: 30''Name: John, Age: 30''Name: John, Age: 30''Name: John, Age: 30'
$matches['name']undefinedundefined'John''John''John''John'
$matches['age']undefinedundefined'30''30''30''30'
Key Moments - 2 Insights
Why does $matches['name'] contain 'John' instead of 'Name'?
The regex named group 'name' captures only the word characters (\w+) after 'Name: ', which is 'John'. See execution_table step 3.
What happens if the regex does not match the string?
The if condition fails, so the script does not extract named groups or print output. See execution_table step 2 where match is checked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $matches['age'] after step 4?
A'30'
B'John'
Cundefined
D'Age'
💡 Hint
Check the 'Result' column in execution_table row for step 4.
At which step does the script confirm the regex matched the string?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table to find where match is checked.
If the input string was 'Name: Alice, Age: 25', what would $matches['name'] be after step 3?
A'John'
B'Alice'
C'Name'
Dundefined
💡 Hint
Refer to variable_tracker and understand how named captures extract the name from the string.
Concept Snapshot
Named captures in PowerShell regex use syntax (?<name>pattern).
Use -match operator to test and extract.
Matched groups are stored in $matches['name'].
Access named groups by their names for clarity.
Useful for extracting parts of strings cleanly.
Full Transcript
This example shows how to use named captures in PowerShell regex. We start with a string containing a name and age. We apply a regex with named groups 'name' and 'age' to extract these parts. The -match operator tests if the regex matches the string. If it does, the named groups are stored in the automatic $matches variable, accessible by their names. We then print a formatted string using these extracted values. This method helps to clearly get parts of strings without counting positions.