Named captures in PowerShell - Time & Space Complexity
We want to understand how the time it takes to find named captures in text grows as the text gets longer.
How does the script's work increase when the input text size changes?
Analyze the time complexity of the following code snippet.
$text = "Name: John, Age: 30; Name: Alice, Age: 25"
$pattern = '(?<name>\w+), Age: (?<age>\d+)'
foreach ($match in [regex]::Matches($text, $pattern)) {
$name = $match.Groups['name'].Value
$age = $match.Groups['age'].Value
Write-Output "Name: $name, Age: $age"
}
This code finds all name and age pairs in the text using named captures and prints them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each match found in the text.
- How many times: Once for each matched name-age pair in the input text.
As the input text grows, the number of matches usually grows roughly in proportion to the text length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 to 2 matches processed |
| 100 | About 10 to 20 matches processed |
| 1000 | About 100 to 200 matches processed |
Pattern observation: The work grows roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the time to find and process named captures grows linearly as the input text gets longer.
[X] Wrong: "Named captures make the search much slower because they add extra work."
[OK] Correct: Named captures just label parts of the match; the main work is still scanning the text once, so the time grows linearly with input size.
Understanding how pattern matching scales helps you write scripts that handle bigger data smoothly and shows you can think about efficiency clearly.
"What if the pattern was more complex and used nested groups? How would the time complexity change?"
Practice
(?<year>\d{4})?Solution
Step 1: Understand named capture syntax
The syntax(?<name>pattern)labels the matched text as 'name'.Step 2: Identify the purpose in the example
Here,(?<year>\d{4})captures 4 digits and labels them 'year'.Final Answer:
Labels the matched part as 'year' for easy access later -> Option DQuick Check:
Named capture = label matched text [OK]
- Thinking it changes match behavior
- Confusing with quantifiers
- Assuming it matches only start of string
Solution
Step 1: Recall PowerShell named capture syntax
PowerShell uses(?<name>pattern)for named captures.Step 2: Match the correct option
(?<day>\d{2})uses(?<day>\d{2}), which is correct syntax for two digits named 'day'.Final Answer:
(?<day>\d{2}) -> Option CQuick Check:
PowerShell named capture = (?<name>pattern) [OK]
- Using Python or other language syntax
- Omitting the question mark
- Using colon instead of angle brackets
$text = 'Date: 2023-07-15'
if ($text -match '(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})') {
"$($matches['year'])/$($matches['month'])/$($matches['day'])"
} else {
'No match'
}Solution
Step 1: Understand the regex and matching
The regex captures year, month, and day as named groups from the string 'Date: 2023-07-15'.Step 2: Access named captures in $matches
After matching, $matches['year'] = '2023', $matches['month'] = '07', $matches['day'] = '15'. The output formats them as "2023/07/15".Final Answer:
"2023/07/15" -> Option AQuick Check:
Named captures accessed = formatted date [OK]
- Expecting original string output
- Using $matches.year instead of $matches['year']
- Not matching regex correctly
$text = 'User: JohnDoe'
if ($text -match '(?<user>\w+)') {
Write-Output $matches.user
}Solution
Step 1: Check regex and variable definitions
The regex and $text variable are correct and properly defined.Step 2: Identify how to access named captures
In PowerShell, named captures are accessed as $matches['name'], not $matches.name.Final Answer:
Incorrect property access; should use $matches['user'] -> Option AQuick Check:
Access named capture with $matches['name'] [OK]
- Using dot notation for $matches named groups
- Assuming regex is wrong
- Ignoring variable initialization
'Name: Jane Doe' using named captures in PowerShell. Which script correctly captures and prints them as "Doe, Jane"?Solution
Step 1: Check regex named capture order
The pattern must capture first name then last name as named groups 'first' and 'last' respectively.Step 2: Verify output formatting and access
Access named captures with $matches['name'] and print as "last, first".$text = 'Name: Jane Doe' if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') { "$($matches['last']), $($matches['first'])" }does this correctly.Final Answer:
The script correctly captures and prints "Doe, Jane" -> Option BQuick Check:
Correct named capture order and access =$text = 'Name: Jane Doe' if ($text -match 'Name: (?<first>\w+) (?<last>\w+)') { "$($matches['last']), $($matches['first'])" }[OK]
- Swapping first and last in regex
- Using unnamed captures but accessing by name
- Using dot notation for $matches
Ready to prove it?
Pass the test and this pattern moves unknown
