Bird
0
0

What will be the output of this PowerShell code?

medium📝 Command Output Q13 of 15
PowerShell - String Operations
What will be the output of this PowerShell code?
$text = 'My phone number is 123-456-7890'
if ($text -match '\d{3}-\d{3}-\d{4}') {
  $matches[0]
} else {
  'No match'
}
ANo match
B123-456-7890
C\d{3}-\d{3}-\d{4}
DMy phone number is 123-456-7890
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex pattern

    The pattern \d{3}-\d{3}-\d{4} matches a phone number format with three digits, a dash, three digits, a dash, and four digits.
  2. Step 2: Check if the string matches and what $matches[0] contains

    The string contains '123-456-7890' which matches the pattern. $matches[0] holds the matched substring, so it outputs '123-456-7890'.
  3. Final Answer:

    123-456-7890 -> Option B
  4. Quick Check:

    Regex match found = matched text [OK]
Quick Trick: Matched text is in $matches[0] after -match success [OK]
Common Mistakes:
  • Expecting the whole string instead of matched part
  • Confusing $matches with the pattern string
  • Not escaping backslashes properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes