0
0
PowerShellscripting~10 mins

Common regex patterns in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common regex patterns
Start with input string
Apply regex pattern
Match found?
NoEnd: No match
Yes
Extract or use matched text
End
This flow shows how a regex pattern is applied to a string to find matches and extract text if found.
Execution Sample
PowerShell
$text = 'Contact: user@example.com'
$pattern = '\b\w+@\w+\.\w+\b'
if ($text -match $pattern) {
  $matches[0]
}
This PowerShell script finds an email address in a string using a regex pattern.
Execution Table
StepActionInputRegex PatternMatch ResultOutput
1Set text variable'Contact: user@example.com'N/AN/A
2Set regex pattern\b\w+@\w+\.\w+\bN/AN/A
3Apply -match operator'Contact: user@example.com'\b\w+@\w+\.\w+\bTrueuser@example.com
4Extract matched textuser@example.comuser@example.com
5EndN/AN/A
💡 Regex matched an email pattern in the input string, extraction complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$textnull'Contact: user@example.com''Contact: user@example.com''Contact: user@example.com''Contact: user@example.com'
$patternnullnull\b\w+@\w+\.\w+\b\b\w+@\w+\.\w+\b\b\w+@\w+\.\w+\b
$matches[0]nullnullnulluser@example.comuser@example.com
Key Moments - 3 Insights
Why does the regex pattern use double backslashes like '\\b' instead of a single backslash?
In PowerShell strings, backslash is an escape character, so to represent a literal backslash in the regex, you need to double it. See Step 2 in the execution_table.
What does the '-match' operator do in PowerShell?
It tests if the input string contains text matching the regex pattern and sets the automatic $matches variable with the found text. Refer to Step 3 and 4 in the execution_table.
How do we access the matched text after using '-match'?
The matched text is stored in $matches[0], as shown in Step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $matches[0] after Step 3?
AContact
Buser@example.com
Cnull
DNo match
💡 Hint
Check the 'Match Result' and 'Output' columns at Step 3 in the execution_table.
At which step does the regex pattern get assigned to the variable $pattern?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for when $pattern is set.
If the input string was 'No email here', what would be the result of the '-match' operator?
Afalse
Buser@example.com
Ctrue
DError
💡 Hint
Refer to the 'Match Result' column in the execution_table and think what happens if no match is found.
Concept Snapshot
Common regex patterns in PowerShell:
- Use -match operator to test regex on strings
- Regex patterns need double backslashes in strings
- Matched text is stored in $matches[0]
- Example: '\b\w+@\w+\.\w+\b' matches emails
- Use if ($text -match $pattern) { $matches[0] } to extract
Full Transcript
This lesson shows how to use common regex patterns in PowerShell. We start with a text string and a regex pattern to find an email address. The pattern uses double backslashes because PowerShell strings treat backslash as escape. Using the -match operator, PowerShell tests if the pattern matches the text. If it does, the matched text is stored in $matches[0]. We traced each step: setting variables, applying the match, and extracting the result. Key points include understanding why double backslashes are needed, how -match works, and how to get the matched text. The quiz checks your understanding of these steps and outcomes.