Bird
Raised Fist0
PowerShellscripting~20 mins

Regex quantifiers and anchors in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Mastery in PowerShell
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?
Consider the following PowerShell command that uses regex anchors and quantifiers. What will be the output?
PowerShell
$text = 'abc123xyz'
if ($text -match '^abc\d{3}xyz$') { 'Match' } else { 'No Match' }
AFalse
BNo Match
CSyntaxError
DMatch
Attempts:
2 left
💡 Hint
The regex checks if the entire string matches the pattern from start (^) to end ($).
💻 Command Output
intermediate
2:00remaining
What does this regex extract from the string?
Given this PowerShell command, what will be the output?
PowerShell
$text = 'Error: 404 Not Found'
if ($text -match 'Error: (\d{3})') { $matches[1] } else { 'No match' }
A404
BError
CNo match
DSyntaxError
Attempts:
2 left
💡 Hint
The regex captures exactly three digits after 'Error: '.
💻 Command Output
advanced
2:00remaining
What is the output of this regex with greedy quantifier?
What will this PowerShell command output?
PowerShell
$text = 'abc123xyz456'
if ($text -match 'abc.*xyz') { $matches[0] } else { 'No match' }
Aabc123xyz
Babc123xyz456
CNo match
DSyntaxError
Attempts:
2 left
💡 Hint
The .* is greedy and matches as many characters as possible but stops at the last 'xyz'.
💻 Command Output
advanced
2:00remaining
What error does this regex cause in PowerShell?
What error will this command produce?
PowerShell
$text = 'abc123'
if ($text -match '[a-z{3}') { 'Match' } else { 'No Match' }
ANo Match
BMatch
CSyntaxError
DRuntimeException
Attempts:
2 left
💡 Hint
Check if the regex pattern is valid with balanced brackets.
💻 Command Output
expert
3:00remaining
What is the output of this complex regex with anchors and quantifiers?
Analyze this PowerShell command and determine its output.
PowerShell
$text = 'start123end'
if ($text -match '^(start)(\d+)(end)$') { "$($matches[1])-$($matches[2])-$($matches[3])" } else { 'No match' }
ANo match
Bstart-123-end
Cstart123end
DSyntaxError
Attempts:
2 left
💡 Hint
The regex uses anchors to match the whole string and captures three groups.

Practice

(1/5)
1. In PowerShell regex, what does the quantifier + mean when used like a+?
easy
A. Matches zero or more occurrences of 'a' in a row
B. Matches one or more occurrences of 'a' in a row
C. Matches exactly one 'a'
D. Matches zero or one occurrence of 'a'

Solution

  1. Step 1: Understand the quantifier '+' meaning

    The '+' quantifier means "one or more" repetitions of the preceding character or group.
  2. Step 2: Apply to the example 'a+'

    So, a+ matches strings with at least one 'a' repeated consecutively.
  3. Final Answer:

    Matches one or more occurrences of 'a' in a row -> Option B
  4. Quick Check:

    '+' means one or more repetitions [OK]
Hint: Remember '+' means at least one repetition [OK]
Common Mistakes:
  • Confusing '+' with '*' which means zero or more
  • Thinking '+' matches exactly one occurrence
  • Mixing '+' with '?' which means zero or one
2. Which of the following is the correct PowerShell regex pattern to match a string that starts with 'test' and ends immediately after?
easy
A. ^test$
B. test^$
C. ^test
D. test$

Solution

  1. Step 1: Understand anchors '^' and '$'

    '^' matches the start of the string, and '$' matches the end of the string.
  2. Step 2: Combine anchors to match exact string

    Using ^test$ ensures the string starts and ends exactly with 'test' and nothing else.
  3. Final Answer:

    ^test$ -> Option A
  4. Quick Check:

    '^' start and '$' end anchors combined [OK]
Hint: Use ^ at start and $ at end for exact match [OK]
Common Mistakes:
  • Placing anchors in wrong order like 'test^$'
  • Using only one anchor and missing full match
  • Confusing anchors with quantifiers
3. What will be the output of this PowerShell command?
$text = 'aaaab';
$text -match '^a{3,5}b$'
medium
A. True
B. False
C. Error
D. Null

Solution

  1. Step 1: Analyze the regex pattern '^a{3,5}b$'

    This pattern matches strings starting with 3 to 5 'a's followed immediately by 'b' and nothing else.
  2. Step 2: Check the string 'aaaab'

    'aaaab' has 4 'a's followed by 'b', which fits the {3,5} quantifier and anchors.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    String matches pattern with 4 'a's and ends with 'b' [OK]
Hint: Count characters and check quantifier range [OK]
Common Mistakes:
  • Ignoring anchors and matching partial strings
  • Miscounting the number of 'a's
  • Confusing {3,5} with exact count
4. Identify the error in this PowerShell regex usage:
$string = 'hello123';
if ($string -match '^hello\d+$') { 'Match' } else { 'No Match' }
medium
A. The pattern should use a single backslash: '^hello\d+$'
B. The quantifier + is invalid here
C. The pattern is correct and will match
D. The anchors ^ and $ are misplaced

Solution

  1. Step 1: Check the regex pattern syntax

    The pattern ^hello\d+$ correctly matches strings starting with 'hello' followed by one or more digits until the end.
  2. Step 2: Verify PowerShell string escaping

    In PowerShell single-quoted strings, backslash is literal, so '\d' is correct to represent digit class.
  3. Final Answer:

    The pattern is correct and will match -> Option C
  4. Quick Check:

    Pattern syntax and anchors are correct [OK]
Hint: Remember PowerShell single quotes treat backslash literally [OK]
Common Mistakes:
  • Thinking double backslash is always wrong in PowerShell
  • Misplacing anchors causing partial matches
  • Assuming quantifiers like '+' are invalid
5. You want to match strings in PowerShell that start with 'cat' followed by zero or more 's', and end exactly there. Which regex pattern should you use?
hard
A. ^cat?s$
B. ^cat+s$
C. cats*$
D. ^cats*$

Solution

  1. Step 1: Understand the requirement

    The string must start with 'cat', then have zero or more 's', and end immediately after.
  2. Step 2: Analyze each pattern

    • ^cats*$: ^cats*$ means start with 'cat', then zero or more 's', end of string -- matches requirement.
    • ^cat+s$: ^cat+s$ means start with 'ca' then one or more 't's, then 's' at end -- incorrect.
    • ^cat?s$: ^cat?s$ means start with 'ca', zero or one 't', then 's' at end -- incorrect.
    • cats*$: cats*$ no start anchor, so can match anywhere -- incorrect.
  3. Final Answer:

    ^cats*$ -> Option D
  4. Quick Check:

    '^cat' start, 's*' zero or more, '$' end [OK]
Hint: Use ^ for start, $ for end, * for zero or more [OK]
Common Mistakes:
  • Using '+' instead of '*' for zero or more
  • Missing anchors causing partial matches
  • Misplacing quantifiers after wrong characters
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