Bird
Raised Fist0
PowerShellscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - Common regex patterns
Start with input string
↓
Apply regex pattern
↓
Match found?
No→End: 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.

Practice

(1/5)
1. Which regex pattern matches any single digit from 0 to 9 in PowerShell?
easy
A. \d
B. \w
C. \s
D. \D

Solution

  1. Step 1: Understand regex digit shorthand

    The pattern \d matches any single digit character (0-9) in regex.
  2. Step 2: Compare other options

    \w matches word characters (letters, digits, underscore), \s matches whitespace, \D matches non-digit characters.
  3. Final Answer:

    \d -> Option A
  4. Quick Check:

    Digit shorthand = \d [OK]
Hint: Remember \d means digit, \w means word character [OK]
Common Mistakes:
  • Confusing \d with \w or \s
  • Using uppercase \D which means non-digit
  • Thinking \w matches only letters
2. Which of the following is the correct PowerShell regex pattern to match a string that starts with 'abc'?
easy
A. abc*
B. abc$
C. ^abc
D. *abc

Solution

  1. Step 1: Understand regex anchors

    The caret ^ matches the start of a string, so ^abc matches strings starting with 'abc'.
  2. Step 2: Check other options

    abc$ matches strings ending with 'abc', abc* matches 'ab' followed by zero or more 'c's, *abc is invalid syntax.
  3. Final Answer:

    ^abc -> Option C
  4. Quick Check:

    Start anchor = ^abc [OK]
Hint: Use ^ at start to match beginning of string [OK]
Common Mistakes:
  • Using $ instead of ^ for start of string
  • Misunderstanding * as wildcard for any chars
  • Placing * at start which is invalid
3. What will be the output of this PowerShell command?
$text = 'User123'
if ($text -match '\w{4}\d{3}') { 'Match' } else { 'No Match' }
medium
A. Match
B. No Match
C. Error
D. false

Solution

  1. Step 1: Analyze the regex pattern

    \w{4} matches exactly 4 word characters, \d{3} matches exactly 3 digits. Together they match 7 characters: 4 word chars + 3 digits.
  2. Step 2: Check the input string

    'User123' has 4 letters 'User' and 3 digits '123', so it matches the pattern.
  3. Final Answer:

    Match -> Option A
  4. Quick Check:

    4 letters + 3 digits = Match [OK]
Hint: Count characters to match {n} quantifiers exactly [OK]
Common Mistakes:
  • Thinking \w includes only letters, ignoring digits
  • Confusing {n} with * or + quantifiers
  • Not escaping backslash properly in PowerShell string
4. Identify the error in this PowerShell regex usage:
$email = 'test@example.com'
if ($email -match '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2}') { 'Valid' } else { 'Invalid' }
medium
A. Missing escape for dot before domain extension
B. Incorrect character class for username
C. Using single quotes prevents regex from working
D. Regex pattern is correct, no error

Solution

  1. Step 1: Review regex pattern components

    The pattern matches username with allowed chars, an @, domain name, a literal dot (escaped), and domain extension of 2+ letters.
  2. Step 2: Check PowerShell string usage

    Single quotes in PowerShell treat string literally, so \ is treated as a single backslash, correctly escaping the dot.
  3. Final Answer:

    Regex pattern is correct, no error -> Option D
  4. Quick Check:

    Proper escaping and pattern = Correct [OK]
Hint: Single quotes keep regex escapes intact in PowerShell [OK]
Common Mistakes:
  • Thinking dot needs double escape in single quotes
  • Assuming single quotes disable regex
  • Misreading character classes as incorrect
5. You want to extract all words starting with a capital letter from a string in PowerShell. Which regex pattern should you use?
hard
A. \b[a-z][A-Z]*\b
B. \b[A-Z][a-z]*\b
C. [A-Z]+\b
D. \b[A-Z]+[0-9]*\b

Solution

  1. Step 1: Understand the requirement

    We want words starting with a capital letter, followed by zero or more lowercase letters.
  2. Step 2: Analyze each pattern

    A: \b[A-Z][a-z]*\b matches word boundary, one uppercase letter, then zero or more lowercase letters, then word boundary - correct.
    B: starts with lowercase letter, so incorrect.
    C: matches one or more uppercase letters but no word boundary start.
    D: matches uppercase letters followed by digits, not only letters.
  3. Final Answer:

    \b[A-Z][a-z]*\b -> Option B
  4. Quick Check:

    Capital start + lowercase letters = \b[A-Z][a-z]*\b [OK]
Hint: Use \b for word boundary and [A-Z] for capital start [OK]
Common Mistakes:
  • Ignoring word boundaries causing partial matches
  • Using [a-z][A-Z]* which starts with lowercase
  • Including digits when only letters needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard