Bird
Raised Fist0
PowerShellscripting~20 mins

Regular expressions with -match in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Regex Match Master
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 code using -match?

Consider this PowerShell snippet:

$text = 'Hello123'
if ($text -match '\d{3}$') { 'Match found' } else { 'No match' }

What will it output?

PowerShell
$text = 'Hello123'
if ($text -match '\d{3}$') { 'Match found' } else { 'No match' }
AMatch found
BNo match
CTrue
DFalse
Attempts:
2 left
💡 Hint

Check if the string ends with exactly three digits.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses -match to check for a word starting with 'a'?

Which PowerShell command correctly checks if the variable $word starts with the letter 'a'?

A$word -match '[a]'
B$word -match 'a$'
C$word -match '^a'
D$word -match 'a*'
Attempts:
2 left
💡 Hint

Use the caret ^ to match the start of a string.

🔧 Debug
advanced
2:00remaining
Why does this -match expression fail to find a match?

Given this code:

$input = 'User-01'
if ($input -match '\w+\d+') { 'Found' } else { 'Not found' }

It outputs 'Not found'. Why?

ABecause -match is case sensitive and input has uppercase letters
BBecause '\w+' matches letters and digits but the pattern expects digits immediately after letters without hyphen
CBecause the backslashes are not escaped properly in the string
DBecause the regex is missing anchors ^ and $
Attempts:
2 left
💡 Hint

Think about what '\w+' matches and the actual string structure.

🚀 Application
advanced
2:00remaining
Which command extracts the first email from a string using -match?

You have this string:

$text = 'Contact us at support@example.com or sales@example.org.'

Which command extracts the first email address correctly?

A$text -match '[\w.-]+@[\w.-]+\.\w{2,}'; $matches[0]
B$text -match '[\w.-]+@[\w.-]+\.\w{2,}'; $matches[1]
C$text -match '\b[\w.-]+@[\w.-]+\.\w{2,}\b'; $matches[1]
D$text -match '\b[\w.-]+@[\w.-]+\.\w{2,}\b'; $matches[0]
Attempts:
2 left
💡 Hint

Remember the automatic variable $matches stores the matched text at index 0.

🧠 Conceptual
expert
3:00remaining
What is the value of $matches after this -match operation?

Run this code:

$string = 'Order #12345 placed'
$string -match 'Order #(\d+)'

What is the value of $matches?

A@{0='Order #12345'; 1='12345'}
B@{0='12345'; 1='Order #12345'}
C@{0='Order #'; 1='12345'}
D@{0='Order #12345'; 1='#12345'}
Attempts:
2 left
💡 Hint

The first element is the full match, the next elements are capture groups.

Practice

(1/5)
1. What does the -match operator do in PowerShell?
easy
A. Converts a string to uppercase
B. Replaces text in a string
C. Splits a string into an array
D. Checks if a string fits a pattern using regular expressions

Solution

  1. Step 1: Understand the purpose of -match

    The -match operator is used to test if a string matches a regular expression pattern.
  2. Step 2: Compare with other string operations

    Replacing text uses -replace, splitting uses -split, and uppercase conversion uses ToUpper(). These are different from -match.
  3. Final Answer:

    Checks if a string fits a pattern using regular expressions -> Option D
  4. Quick Check:

    -match = pattern check [OK]
Hint: Remember: -match means 'does it fit the pattern?' [OK]
Common Mistakes:
  • Confusing -match with -replace or -split
  • Thinking -match changes the string
  • Assuming -match returns the matched text directly
2. Which of the following is the correct syntax to check if the variable $text contains the word 'hello' using -match?
easy
A. if ($text contains 'hello')
B. match $text 'hello'
C. $text -match 'hello'
D. $text =~ 'hello'

Solution

  1. Step 1: Recall PowerShell -match syntax

    The correct syntax is $variable -match 'pattern' to test if the variable matches the pattern.
  2. Step 2: Evaluate each option

    $text -match 'hello' uses the correct syntax. match $text 'hello' is invalid syntax. $text =~ 'hello' uses a Bash-like operator, not PowerShell. if ($text contains 'hello') uses a wrong operator for matching.
  3. Final Answer:

    $text -match 'hello' -> Option C
  4. Quick Check:

    Correct syntax = $text -match 'hello' [OK]
Hint: Use $var -match 'pattern' to test matches [OK]
Common Mistakes:
  • Using Bash or other language operators like =~
  • Writing match before the variable
  • Using 'contains' instead of -match
3. What will be the output of this PowerShell code?
$text = 'My phone number is 123-456-7890'
if ($text -match '\d{3}-\d{3}-\d{4}') {
  $matches[0]
} else {
  'No match'
}
medium
A. No match
B. 123-456-7890
C. \d{3}-\d{3}-\d{4}
D. My phone number is 123-456-7890

Solution

  1. Step 1: Understand the regex pattern

    The pattern \d{3}-\d{3}-\d{4} matches a phone number format with three digits, a dash, three digits, a dash, and four digits.
  2. Step 2: Check if the string matches and what $matches[0] contains

    The string contains '123-456-7890' which matches the pattern. $matches[0] holds the matched substring, so it outputs '123-456-7890'.
  3. Final Answer:

    123-456-7890 -> Option B
  4. Quick Check:

    Regex match found = matched text [OK]
Hint: Matched text is in $matches[0] after -match success [OK]
Common Mistakes:
  • Expecting the whole string instead of matched part
  • Confusing $matches with the pattern string
  • Not escaping backslashes properly
4. Identify the error in this PowerShell snippet:
$text = 'Error code: 404'
if ($text -match '\d{3}') {
  Write-Output $matches
}
medium
A. You must use $matches[0] to get the matched string
B. The regex pattern is invalid
C. $matches is an automatic variable, no error here
D. You need to use -like instead of -match

Solution

  1. Step 1: Understand $matches variable usage

    $matches is a hashtable holding matched groups. To get the matched string, you must access $matches[0].
  2. Step 2: Analyze the code output

    Using Write-Output $matches prints the whole hashtable, not the matched string. To print the matched text, use Write-Output $matches[0].
  3. Final Answer:

    You must use $matches[0] to get the matched string -> Option A
  4. Quick Check:

    Print matched text with $matches[0] [OK]
Hint: Use $matches[0] to print matched text, not $matches alone [OK]
Common Mistakes:
  • Printing $matches directly instead of $matches[0]
  • Thinking $matches is a string, not a hashtable
  • Confusing -match with -like operator
5. You want to extract all email addresses from a list of strings stored in $lines. Which PowerShell snippet correctly uses -match and $matches to collect emails into $emails?
hard
A. $emails = foreach ($line in $lines) { if ($line -match '[\w.-]+@[\w.-]+\.\w+') { $matches[0] } }
B. $emails = $lines -match '[\w.-]+@[\w.-]+\.\w+'
C. $emails = $lines | Where-Object { $_ -contains '[\w.-]+@[\w.-]+\.\w+' }
D. $emails = foreach ($line in $lines) { $line -match '[\w.-]+@[\w.-]+\.\w+' }

Solution

  1. Step 1: Understand how to loop and match each line

    We must check each line with -match and if it matches, collect the matched email from $matches[0].
  2. Step 2: Evaluate each option

    $emails = foreach ($line in $lines) { if ($line -match '[\w.-]+@[\w.-]+\.\w+') { $matches[0] } } loops through lines, tests with -match, and collects matched emails correctly. $emails = $lines -match '[\w.-]+@[\w.-]+\.\w+' tries to match the whole array at once, which doesn't work. $emails = $lines | Where-Object { $_ -contains '[\w.-]+@[\w.-]+\.\w+' } uses -contains which does not support regex. $emails = foreach ($line in $lines) { $line -match '[\w.-]+@[\w.-]+\.\w+' } runs -match but does not collect matched emails.
  3. Final Answer:

    $emails = foreach ($line in $lines) { if ($line -match '[\w.-]+@[\w.-]+\.\w+') { $matches[0] } } -> Option A
  4. Quick Check:

    Loop + -match + $matches[0] collects matches [OK]
Hint: Use foreach + if (-match) + $matches[0] to collect matches [OK]
Common Mistakes:
  • Trying to match the whole array at once
  • Using -contains instead of -match for regex
  • Not collecting $matches[0] inside the loop
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