0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.