0
0
PowerShellscripting~10 mins

Why regex enables pattern matching in PowerShell - Test Your Understanding

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 sat on the mat'
if ($text -match [1]) {
    Write-Output 'Match found'
}
Drag options to blanks, or click blank then click option'
A'cat'
B'rat'
C'bat'
D'dog'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a word not present in the string, so no match occurs.
2fill in blank
medium

Complete the code to find if the string starts with a digit using regex.

PowerShell
$text = '9 lives'
if ($text -match [1]) {
    Write-Output 'Starts with a digit'
}
Drag options to blanks, or click blank then click option'
A'^\d'
B'[a-z]'
C'\d$'
D'^cat'
Attempts:
3 left
💡 Hint
Common Mistakes
Using \d$ which checks for digit at the end, not start.
3fill in blank
hard

Fix the error in the regex pattern to match any word ending with 'ing'.

PowerShell
$text = 'I am running fast'
if ($text -match [1]) {
    Write-Output 'Found a word ending with ing'
}
Drag options to blanks, or click blank then click option'
A'\w+ing$'
B'\bing\b'
C'ing$'
D'\w+ing\b'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ing$' matches only if 'ing' is at the end of the string, not word.
4fill in blank
hard

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

PowerShell
$text = 'My code is 123'
if ($text -match [1]) {
    Write-Output 'Contains exactly 3 digits'
}

$pattern = [2]
Drag options to blanks, or click blank then click option'
A'\b\d{3}\b'
B'\d{2}'
C'\d{3}'
D'\b\d{2}\b'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {2} matches only 2 digits, not 3.
5fill in blank
hard

Fill all three blanks to create a hashtable of words and their lengths for words longer than 4 letters.

PowerShell
$words = @('apple', 'bat', 'grape', 'kiwi')
$lengths = @{ [1] : [2] for [3] in $words if $[3].Length -gt 4 }
Write-Output $lengths
Drag options to blanks, or click blank then click option'
A$word
B$word.Length
Cword
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causing errors.