0
0
PowerShellscripting~10 mins

Common regex patterns in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a string contains only digits using regex.

PowerShell
$string = "12345"
if ($string -match "^[1]$") {
    Write-Output "Only digits"
}
Drag options to blanks, or click blank then click option'
A\d+
B\w+
C\s+
D\D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using \w+ matches letters and digits, not digits only.
Using \s+ matches whitespace, not digits.
2fill in blank
medium

Complete the code to find all words starting with a capital letter.

PowerShell
$text = "Hello World from PowerShell"
$matches = [regex]::Matches($text, "\b[1]\w*")
$matches | ForEach-Object { $_.Value }
Drag options to blanks, or click blank then click option'
A\d
B[a-z]
C[A-Z]
D\s
Attempts:
3 left
💡 Hint
Common Mistakes
Using [a-z] matches lowercase letters, not capital letters.
Using \d matches digits, not letters.
3fill in blank
hard

Fix the error in the regex pattern to match email addresses.

PowerShell
$email = "user@example.com"
if ($email -match "^[\w.-]+@[1]\.[a-z]{2,}$") {
    Write-Output "Valid email"
}
Drag options to blanks, or click blank then click option'
A[\w-]+
B[\d]+
C[\s]+
D[A-Z]+
Attempts:
3 left
💡 Hint
Common Mistakes
Using [\d]+ matches only digits, not domain names.
Using [\s]+ matches spaces, invalid in emails.
4fill in blank
hard

Fill both blanks to create a regex that matches a US phone number format (e.g., 123-456-7890).

PowerShell
$phone = "123-456-7890"
if ($phone -match "^[1]-[2]-\d{4}$") {
    Write-Output "Valid phone number"
}
Drag options to blanks, or click blank then click option'
A\d{3}
B\w{3}
C\d{4}
D\s{3}
Attempts:
3 left
💡 Hint
Common Mistakes
Using \w{3} matches letters and digits, not just digits.
Using \s{3} matches spaces, not digits.
5fill in blank
hard

Fill both blanks to create a regex that matches a hex color code (e.g., #1A2B3C).

PowerShell
$color = "#1A2B3C"
if ($color -match "^[1][2]$") {
    Write-Output "Valid hex color"
}
Drag options to blanks, or click blank then click option'
A#
B[0-9A-Fa-f]{6}
C[0-9A-Fa-f]
D[A-Fa-f0-9]{3}
Attempts:
3 left
💡 Hint
Common Mistakes
Using [A-Fa-f0-9]{3} matches only 3 digits, not 6.
Missing the # at the start.