0
0
PowerShellscripting~10 mins

Regular expressions with -match in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Regular expressions with -match
Input String
Apply -match with regex
Check if pattern matches?
NoOutput: False
Yes
Output: True
Store matched substring in $matches
Use $matches for further processing
The flow shows how PowerShell uses -match to test if a string fits a pattern, outputs True/False, and stores matched parts.
Execution Sample
PowerShell
$text = 'Hello123'
if ($text -match '\d+') {
  $matches[0]
} else {
  'No match'
}
This code checks if $text contains digits and outputs the matched digits or 'No match'.
Execution Table
StepActionInputRegex PatternMatch ResultOutput$matches[0]
1Set variable $text'Hello123'
2Evaluate condition $text -match '\d+''Hello123'\d+True
3Store matched substring in $matches123
4Output matched substring123123
💡 Pattern '\d+' found digits in 'Hello123', so condition is True and matched substring '123' is output.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$textundefined'Hello123''Hello123''Hello123'
$matchesundefinedundefined@{0='123'}@{0='123'}
Key Moments - 3 Insights
Why does the condition return True even though the whole string is not digits?
Because -match checks if any part of the string fits the pattern. Here '\d+' matches the '123' part inside 'Hello123' (see execution_table step 2).
What is stored in $matches after a successful match?
$matches[0] stores the exact substring that matched the pattern. In this example, it is '123' (see execution_table step 3).
What happens if the pattern does not match any part of the string?
The condition returns False, and $matches is not updated. The else branch runs (not shown here but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $matches[0] after step 3?
A'Hello123'
B'123'
C'' (empty string)
DUndefined
💡 Hint
Check the 'Match Result' and '$matches[0]' columns in execution_table row for step 3.
At which step does the condition $text -match '\d+' evaluate to True?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Match Result' column in execution_table for each step.
If $text was 'Hello' (no digits), what would the output be?
A'No match'
B'' (empty string)
C'Hello'
D'123'
💡 Hint
Recall that if -match fails, the else branch outputs 'No match' as per execution_sample.
Concept Snapshot
PowerShell -match tests if a string fits a regex pattern.
Returns True if any part matches, else False.
Matched substring stored in $matches[0].
Use $matches to access matched text.
Example: 'abc123' -match '\d+' is True, $matches[0] = '123'.
Full Transcript
This lesson shows how PowerShell uses the -match operator to check if a string contains a pattern defined by a regular expression. The process starts by setting a string variable. Then, the -match operator tests if the string contains digits using the pattern '\d+'. If the pattern is found anywhere in the string, the condition returns True. The matched substring is saved in the automatic variable $matches at index 0. Finally, the matched substring is output. If no match is found, the condition returns False and the else branch runs. This helps beginners understand how to find patterns inside strings and use the matched parts for further work.