Consider this PowerShell snippet:
$text = 'Hello123'
if ($text -match '\d{3}$') { 'Match found' } else { 'No match' }What will it output?
$text = 'Hello123' if ($text -match '\d{3}$') { 'Match found' } else { 'No match' }
Check if the string ends with exactly three digits.
The regex '\d{3}$' looks for exactly three digits at the end of the string. 'Hello123' ends with '123', so it matches.
Which PowerShell command correctly checks if the variable $word starts with the letter 'a'?
Use the caret ^ to match the start of a string.
Regex '^a' matches strings starting with 'a'. Option C matches strings ending with 'a'. Option C matches zero or more 'a's anywhere. Option C matches any string containing 'a'.
Given this code:
$input = 'User-01'
if ($input -match '\w+\d+') { 'Found' } else { 'Not found' }It outputs 'Not found'. Why?
Think about what '\w+' matches and the actual string structure.
The pattern '\w+\d+' expects one or more word characters followed immediately by one or more digits. 'User-01' has a hyphen between letters and digits, so the digits are not immediately after letters, causing no match.
You have this string:
$text = 'Contact us at support@example.com or sales@example.org.'
Which command extracts the first email address correctly?
Remember the automatic variable $matches stores the matched text at index 0.
Option D uses word boundaries and captures the first email correctly. $matches[0] contains the matched string. Other options use wrong indices or miss word boundaries.
Run this code:
$string = 'Order #12345 placed'
$string -match 'Order #(\d+)'
What is the value of $matches?
The first element is the full match, the next elements are capture groups.
$matches[0] holds the entire matched string 'Order #12345'. $matches[1] holds the first captured group '12345'.