Bird
Raised Fist0
PowerShellscripting~20 mins

Why regex enables pattern matching in PowerShell - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell regex match?
Given the following PowerShell code, what is the output?
PowerShell
$text = 'cat bat rat'
if ($text -match '\b[a-z]at\b') { $matches[0] } else { 'No match' }
Acat
Bbat
Crat
DNo match
Attempts:
2 left
💡 Hint
The -match operator returns the first match found in the string.
🧠 Conceptual
intermediate
2:00remaining
Why does regex enable flexible pattern matching?
Which statement best explains why regex is powerful for pattern matching in scripts?
ARegex uses special symbols to define complex search patterns.
BRegex can only match numbers and letters but not symbols.
CRegex allows matching exact strings only without variations.
DRegex requires manual checking of each character in a string.
Attempts:
2 left
💡 Hint
Think about how regex can match different patterns, not just fixed text.
📝 Syntax
advanced
2:00remaining
Which PowerShell regex pattern matches an email address?
Select the regex pattern that correctly matches a simple email format in PowerShell.
A'[a-z]+@[0-9]+\.[a-z]{2}'
B'[a-zA-Z]+\s@[a-zA-Z]+\.[a-z]{3}'
C'^\d+@\w+\.com$'
D'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$'
Attempts:
2 left
💡 Hint
Look for a pattern that allows letters, digits, dots, and hyphens before and after '@', and a domain suffix.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell regex fail to match?
Given this code, why does it not match the word 'hello'? $text = 'hello world' if ($text -match '[h-e]+') { 'Match found' } else { 'No match' }
AThe regex should use double quotes instead of single quotes.
BThe regex is missing anchors ^ and $ to match the whole word.
CThe character range [h-e] is invalid because 'h' comes after 'e'.
DPowerShell -match operator does not support character ranges.
Attempts:
2 left
💡 Hint
Check the order of characters inside the square brackets for ranges.
🚀 Application
expert
2:00remaining
How many matches does this PowerShell regex find?
Count how many matches this regex finds in the string: $text = 'one1 two2 three3 four4' $matches = [regex]::Matches($text, '\b\w+\d\b') $matches.Count
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Look for words ending with a digit, separated by word boundaries.

Practice

(1/5)
1. What is the main reason regex enables pattern matching in PowerShell?
easy
A. It allows searching text using flexible patterns instead of exact words.
B. It runs faster than all other PowerShell commands.
C. It only works with numbers, not text.
D. It automatically fixes spelling mistakes in text.

Solution

  1. Step 1: Understand regex purpose

    Regex is designed to find text that matches a pattern, not just exact words.
  2. Step 2: Compare options

    Only It allows searching text using flexible patterns instead of exact words. correctly describes regex's ability to match flexible text patterns.
  3. Final Answer:

    It allows searching text using flexible patterns instead of exact words. -> Option A
  4. Quick Check:

    Regex = flexible text search [OK]
Hint: Regex matches patterns, not exact text [OK]
Common Mistakes:
  • Thinking regex only works with numbers
  • Believing regex fixes spelling errors
  • Assuming regex is always faster than other commands
2. Which of the following is the correct PowerShell syntax to check if the string 'Hello123' contains digits using regex?
easy
A. 'Hello123' -contains '\d+'
B. 'Hello123' -match '[a-z]+'
C. 'Hello123' -match '\d+'
D. 'Hello123' -like '*\d+'

Solution

  1. Step 1: Identify correct operator and pattern

    PowerShell uses '-match' to test regex patterns. '\d+' matches one or more digits.
  2. Step 2: Check options

    'Hello123' -match '\d+' uses '-match' with '\d+' correctly. Others use wrong operators or patterns.
  3. Final Answer:

    'Hello123' -match '\d+' -> Option C
  4. Quick Check:

    Use '-match' with '\d+' for digits [OK]
Hint: Use '-match' with '\d+' to find digits [OK]
Common Mistakes:
  • Using '-contains' instead of '-match'
  • Using '-like' with regex patterns
  • Forgetting to escape backslash in regex
3. What will be the output of this PowerShell command?
'PowerShell123' -match '\d{3}'
medium
A. false
B. true
C. 123
D. Error

Solution

  1. Step 1: Understand the regex pattern

    '\d{3}' matches exactly three digits in a row.
  2. Step 2: Check the string for pattern

    'PowerShell123' contains '123' which matches three digits, so '-match' returns true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Digits '123' found = true [OK]
Hint: '-match' returns true if pattern found [OK]
Common Mistakes:
  • Expecting the matched text instead of true/false
  • Confusing '-match' with '-replace'
  • Thinking it returns false if digits are at the end
4. Identify the error in this PowerShell command that tries to check if 'Test123' contains letters:
'Test123' -match '[A-Z]+'
medium
A. The regex pattern should be '\\d+' to match letters.
B. The '-match' operator is incorrect; should use '-contains'.
C. The string 'Test123' is not quoted properly.
D. The regex pattern only matches uppercase letters, missing lowercase letters.

Solution

  1. Step 1: Analyze the regex pattern

    '[A-Z]+' matches only uppercase letters, but 'Test123' has lowercase 'e', 's', 't'.
  2. Step 2: Identify the error

    Because pattern misses lowercase letters, it won't match 'Test123' correctly.
  3. Final Answer:

    The regex pattern only matches uppercase letters, missing lowercase letters. -> Option D
  4. Quick Check:

    Pattern misses lowercase letters = Error [OK]
Hint: Use [a-zA-Z] to match all letters [OK]
Common Mistakes:
  • Using '-contains' instead of '-match'
  • Ignoring case sensitivity in regex
  • Confusing digits '\\d' with letters
5. You want to extract all words starting with 'a' or 'A' from the string 'Apple and apricot are awesome'. Which PowerShell command correctly uses regex to do this?
hard
A. [regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*') | ForEach-Object { $_.Value }
B. 'Apple and apricot are awesome' -replace '\b[aA]\w*', ''
C. 'Apple and apricot are awesome' -match '\b[aA]\w*'
D. [regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*').Value

Solution

  1. Step 1: Understand the goal and regex

    The pattern '\b[aA]\w*' matches words starting with 'a' or 'A'.
  2. Step 2: Check PowerShell methods

    '-match' returns true/false, not matches. '-replace' removes text. [regex]::Matches returns all matches, but .Value returns only first match. Using ForEach-Object extracts all matched words.
  3. Final Answer:

    [regex]::Matches('Apple and apricot are awesome', '\b[aA]\w*') | ForEach-Object { $_.Value } -> Option A
  4. Quick Check:

    Use [regex]::Matches with ForEach-Object for all matches [OK]
Hint: Use [regex]::Matches and loop to get all matches [OK]
Common Mistakes:
  • Using '-match' which returns only true/false
  • Using '-replace' which removes matched text
  • Using .Value on Matches returns only first match
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