0
0
PowerShellscripting~10 mins

Regular expressions with -match 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 the string contains the word 'cat'.

PowerShell
$text = 'The cat is sleeping.'
if ($text [1] 'cat') {
    Write-Output 'Found a cat!'
}
Drag options to blanks, or click blank then click option'
A-contains
B-match
C-like
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-contains' which is for collections, not strings.
Using '-eq' which checks for exact equality.
2fill in blank
medium

Complete the code to check if the string starts with a digit using a regular expression.

PowerShell
$text = '3 cats'
if ($text [1] '^[0-9]') {
    Write-Output 'Starts with a digit'
}
Drag options to blanks, or click blank then click option'
A-like
B-eq
C-match
D-contains
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which does not support full regex.
Using '-contains' which is for collections.
3fill in blank
hard

Fix the error in the code to check if the string ends with '.txt'.

PowerShell
$filename = 'document.txt'
if ($filename [1] '\.txt$') {
    Write-Output 'Text file detected'
}
Drag options to blanks, or click blank then click option'
A-like
B-eq
C-contains
D-match
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which does not support regex anchors like '$'.
Using '-eq' which checks exact equality.
4fill in blank
hard

Fill both blanks to create a regex that matches strings with exactly 3 digits.

PowerShell
$input = '123'
if ($input [1] '^[0-9][2]$') {
    Write-Output 'Exactly three digits'
}
Drag options to blanks, or click blank then click option'
A-match
B-like
C{3}
D{2}
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which does not support regex quantifiers.
Using '{2}' which matches exactly two digits.
5fill in blank
hard

Fill all three blanks to create a regex that matches strings starting with 'a' or 'b' followed by one or more digits.

PowerShell
$text = 'a123'
if ($text [1] '^[[2]][0-9][3]$') {
    Write-Output 'Match found'
}
Drag options to blanks, or click blank then click option'
A-match
Bab
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which means zero or more, not one or more.
Using '-like' which does not support regex.