Bird
0
0

Identify the error in this PowerShell regex usage:

medium📝 Debug Q14 of 15
PowerShell - Regular Expressions
Identify the error in this PowerShell regex usage:
$string = 'hello123';
if ($string -match '^hello\d+$') { 'Match' } else { 'No Match' }
AThe pattern should use a single backslash: '^hello\d+$'
BThe quantifier + is invalid here
CThe pattern is correct and will match
DThe anchors ^ and $ are misplaced
Step-by-Step Solution
Solution:
  1. Step 1: Check the regex pattern syntax

    The pattern ^hello\d+$ correctly matches strings starting with 'hello' followed by one or more digits until the end.
  2. Step 2: Verify PowerShell string escaping

    In PowerShell single-quoted strings, backslash is literal, so '\d' is correct to represent digit class.
  3. Final Answer:

    The pattern is correct and will match -> Option C
  4. Quick Check:

    Pattern syntax and anchors are correct [OK]
Quick Trick: Remember PowerShell single quotes treat backslash literally [OK]
Common Mistakes:
  • Thinking double backslash is always wrong in PowerShell
  • Misplacing anchors causing partial matches
  • Assuming quantifiers like '+' are invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes