Complete the code to check if the string contains the word 'cat'.
$text = 'The cat sat on the mat' if ($text -match [1]) { Write-Output 'Match found' }
The -match operator checks if the string contains the pattern. Here, 'cat' is the pattern to find.
Complete the code to find if the string starts with a digit using regex.
$text = '9 lives' if ($text -match [1]) { Write-Output 'Starts with a digit' }
The regex ^\d means the string starts (^) with a digit (\d).
Fix the error in the regex pattern to match any word ending with 'ing'.
$text = 'I am running fast' if ($text -match [1]) { Write-Output 'Found a word ending with ing' }
The pattern \w+ing\b matches words ending with 'ing'. \w+ means letters before 'ing', and \b is word boundary.
Fill both blanks to create a regex that matches strings with exactly 3 digits.
$text = 'My code is 123' if ($text -match [1]) { Write-Output 'Contains exactly 3 digits' } $pattern = [2]
The pattern \b\d{3}\b matches exactly 3 digits as a whole word. \d{3} means 3 digits in a row.
Fill all three blanks to create a hashtable of words and their lengths for words longer than 4 letters.
$words = @('apple', 'bat', 'grape', 'kiwi') $lengths = @{ [1] : [2] for [3] in $words if $[3].Length -gt 4 } Write-Output $lengths
The hashtable comprehension uses word as the key, $word.Length as the value, and iterates over word in $words. It filters words longer than 4 letters.