Challenge - 5 Problems
Regex Master with Select-String
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of Select-String with simple regex
What is the output of this PowerShell command when run on a file containing the lines:
Command:
apple
banana
apricot
grape
Command:
Select-String -Pattern '^a' -Path 'fruits.txt'
PowerShell
Select-String -Pattern '^a' -Path 'fruits.txt'
Attempts:
2 left
💡 Hint
The regex '^a' matches lines starting with 'a'.
✗ Incorrect
The pattern '^a' matches lines that start with the letter 'a'. In the file, 'apple' and 'apricot' start with 'a', so Select-String outputs those lines.
💻 Command Output
intermediate2:00remaining
Select-String with case-insensitive regex
Given a file 'colors.txt' with lines:
What is the output of:
Red
blue
GREEN
Yellow
What is the output of:
Select-String -Pattern 'green' -Path 'colors.txt' -CaseSensitive:$false
PowerShell
Select-String -Pattern 'green' -Path 'colors.txt' -CaseSensitive:$false
Attempts:
2 left
💡 Hint
CaseSensitive:$false makes the search ignore case.
✗ Incorrect
The pattern 'green' matches 'GREEN' ignoring case because CaseSensitive is set to false. So the output is the line 'GREEN'.
📝 Syntax
advanced2:00remaining
Identify the correct regex pattern syntax in Select-String
Which option correctly matches lines containing exactly three digits in a row using Select-String?
PowerShell
Select-String -Pattern '<pattern>' -Path 'numbers.txt'
Attempts:
2 left
💡 Hint
Use character classes and quantifiers correctly.
✗ Incorrect
Option A '[0-9]{3}' matches exactly three digits in a row. Option A is invalid in PowerShell because '\d' is not recognized as a digit shorthand in Select-String regex. Option A matches three or more digits, and D is invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this Select-String command fail?
You run this command:
But it returns no matches even though the file contains emails like 'user@example.com'. Why?
Select-String -Pattern '\w+@\w+\.com' -Path 'emails.txt'
But it returns no matches even though the file contains emails like 'user@example.com'. Why?
PowerShell
Select-String -Pattern '\w+@\w+\.com' -Path 'emails.txt'
Attempts:
2 left
💡 Hint
Check if '\w' is recognized in PowerShell regex.
✗ Incorrect
PowerShell's Select-String uses .NET regex which supports '\w', but in PowerShell strings, '\w' needs to be escaped properly or replaced with '[a-zA-Z0-9_]'. Using '\w' inside single quotes is safer. The issue is usually the escape sequence in the string, not the regex itself.
🚀 Application
expert3:00remaining
Extracting matched text with Select-String and regex groups
You want to extract only the domain part from email addresses in 'emails.txt' using Select-String with regex groups. Which command correctly outputs just the domain (e.g., 'example.com')?
PowerShell
Select-String -Pattern '<pattern>' -Path 'emails.txt' | ForEach-Object { <extract> }
Attempts:
2 left
💡 Hint
Check how to access regex groups from Select-String Matches property.
✗ Incorrect
In PowerShell, Select-String returns MatchInfo objects with a Matches collection. Each Match has Groups. The first match is at index 0, and the first group is at Groups[1]. So $_.Matches[0].Groups[1].Value extracts the domain part.