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:
$email = 'test@example.com'
if ($email -match '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2}') { 'Valid' } else { 'Invalid' }
AMissing escape for dot before domain extension
BIncorrect character class for username
CUsing single quotes prevents regex from working
DRegex pattern is correct, no error
Step-by-Step Solution
Solution:
  1. Step 1: Review regex pattern components

    The pattern matches username with allowed chars, an @, domain name, a literal dot (escaped), and domain extension of 2+ letters.
  2. Step 2: Check PowerShell string usage

    Single quotes in PowerShell treat string literally, so \ is treated as a single backslash, correctly escaping the dot.
  3. Final Answer:

    Regex pattern is correct, no error -> Option D
  4. Quick Check:

    Proper escaping and pattern = Correct [OK]
Quick Trick: Single quotes keep regex escapes intact in PowerShell [OK]
Common Mistakes:
  • Thinking dot needs double escape in single quotes
  • Assuming single quotes disable regex
  • Misreading character classes as incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes