Switch with wildcard and regex in PowerShell - Time & Space Complexity
We want to understand how the time it takes to run a switch statement with wildcard and regex patterns changes as we add more items to check.
Specifically, how does the number of checks grow when the input list gets bigger?
Analyze the time complexity of the following code snippet.
$samples = @('apple', 'banana', 'cherry', 'date')
foreach ($item in $samples) {
switch -Wildcard ($item) {
'a*' { Write-Output "Starts with a: $item" }
'*n*' { Write-Output "Contains n: $item" }
default { Write-Output "No match: $item" }
}
}
This code checks each item in a list against wildcard patterns using a switch statement and prints a message based on the match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the list and checking it against each switch pattern.
- How many times: For each item, the switch checks patterns one by one until a match is found or all patterns are checked.
As the list of items grows, the number of checks grows roughly in proportion to the number of items times the number of patterns.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the number of patterns |
| 100 | About 100 times the number of patterns |
| 1000 | About 1000 times the number of patterns |
Pattern observation: The total checks grow linearly as the number of items increases.
Time Complexity: O(n)
This means the time to run grows directly with the number of items you check.
[X] Wrong: "Using wildcard or regex makes the switch run in constant time regardless of input size."
[OK] Correct: Each item still needs to be checked against each pattern, so time grows with the number of items.
Understanding how pattern matching scales helps you write scripts that stay fast even with bigger data. This skill shows you can think about efficiency, not just correctness.
"What if we added more patterns inside the switch? How would the time complexity change?"
Practice
-Wildcard parameter do in a PowerShell switch statement?Solution
Step 1: Understand the role of -Wildcard
The-Wildcardparameter allows pattern matching using simple wildcards such as * (any characters) and ? (single character).Step 2: Differentiate from -Regex
Unlike-Regex, which uses complex regular expressions,-Wildcardis simpler and easier for basic pattern matching.Final Answer:
It matches patterns using simple wildcards like * and ?. -> Option DQuick Check:
-Wildcard = simple wildcard matching [OK]
- Confusing -Wildcard with -Regex
- Thinking -Wildcard ignores case by default
- Assuming -Wildcard speeds up execution
switch statement with wildcard matching in PowerShell?Solution
Step 1: Identify correct parameter placement
In PowerShell, the-Wildcardparameter is placed immediately afterswitchand before the input expression.Step 2: Check the pattern and block syntax
The pattern '*test*' and the script block '{ 'Match' }' are correctly placed inside the switch braces.Final Answer:
switch -Wildcard ($input) { '*test*' { 'Match' } } -> Option CQuick Check:
Parameter right after switch keyword = switch -Wildcard ($input) { '*test*' { 'Match' } } [OK]
- Placing -Wildcard inside the switch block
- Putting -Wildcard after the input variable
- Using -Wildcard as a pattern inside braces
switch -Regex ('apple', 'banana', 'cherry') {
'^a' { 'Starts with a' }
'an' { 'Contains an' }
default { 'No match' }
}Solution
Step 1: Check each input against regex patterns
'apple' matches '^a' (starts with 'a'), so outputs 'Starts with a'. 'banana' contains 'an', so matches 'an' pattern, outputs 'Contains an'. 'cherry' matches none, so outputs 'No match'.Step 2: Confirm output order
The outputs correspond to each input in order: 'apple' -> 'Starts with a', 'banana' -> 'Contains an', 'cherry' -> 'No match'.Final Answer:
Starts with a Contains an No match -> Option AQuick Check:
Regex matches each input correctly = Starts with a Contains an No match [OK]
- Assuming only first match per input
- Confusing regex anchors like ^
- Ignoring default case output
switch statement using wildcard matching:switch ($word) -Wildcard {
'he*' { 'Starts with he' }
'*lo' { 'Ends with lo' }
default { 'No match' }
}Solution
Step 1: Check parameter placement
The-Wildcardparameter must come immediately after theswitchkeyword, not after the input variable.Step 2: Validate patterns and default block
The patterns 'he*' and '*lo' are valid wildcards. The default case has a proper script block. Using wildcards with variables is allowed.Final Answer:
The -Wildcard parameter should be before the input variable. -> Option AQuick Check:
Parameter placement error = The -Wildcard parameter should be before the input variable. [OK]
- Putting -Wildcard after input variable
- Thinking wildcards are invalid patterns
- Omitting script block braces
switch with both wildcard and regex patterns. Which approach correctly combines -Wildcard and -Regex in one script to handle different patterns?Solution
Step 1: Understand parameter exclusivity
PowerShell'sswitchdoes not support using-Wildcardand-Regextogether in one statement. They are mutually exclusive parameters.Step 2: Choose practical approach
To handle both wildcard and regex patterns, use two separateswitchstatements: one with-Wildcardand one with-Regex, processing the list twice.Final Answer:
Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. -> Option BQuick Check:
Separate switches for wildcard and regex = Use two separate switch statements: one with -Wildcard, one with -Regex, processing the list twice. [OK]
- Trying to combine -Wildcard and -Regex in one switch
- Assuming pattern modifiers inside switch block
- Converting wildcards manually instead of separate switches
Ready to prove it?
Pass the test and this pattern moves unknown
