📖 Scenario: You work in IT support and need to filter log entries to find specific patterns. Logs contain timestamps and messages. You want to find lines that start with a date and time, and contain a specific word repeated a certain number of times.
🎯 Goal: Build a PowerShell script that uses regex quantifiers and anchors to match log lines starting with a date and time, and containing a repeated word pattern.
📋 What You'll Learn
Create a variable $logLines with 3 exact log entries
Create a variable $pattern with a regex pattern using anchors and quantifiers
Use Where-Object with -match operator and $pattern to filter matching lines
Print the matching lines using Write-Output
💡 Why This Matters
🌍 Real World
Filtering log files to find specific error patterns helps IT support quickly identify issues.
💼 Career
Regex skills are essential for scripting and automation tasks in system administration and data processing.
Progress0 / 4 steps
1
Create the log lines data
Create a variable called $logLines and assign it an array with these exact strings: '2024-06-01 10:00:00 INFO Start process', '2024-06-01 10:05:00 ERROR Error Error Error occurred', and '2024-06-01 10:10:00 INFO End process'.
PowerShell
Hint
Use @( ... ) to create an array in PowerShell.
2
Create the regex pattern with anchors and quantifiers
Create a variable called $pattern and assign it a regex string that matches lines starting with a date and time in the format YYYY-MM-DD HH:MM:SS (use ^ anchor), followed by a space, then the word ERROR, then a space, then the word Error repeated exactly 3 times separated by spaces, using quantifiers, and then any text after.
PowerShell
Hint
Use ^ to anchor start, \d{4} for 4 digits, and (Error ){3} to repeat 'Error ' exactly 3 times.
3
Filter log lines using the regex pattern
Use Where-Object with -match operator and the variable $pattern to filter $logLines for matching lines. Store the result in a variable called $matchingLines.
PowerShell
Hint
Use $matchingLines = $logLines | Where-Object { $_ -match $pattern } to filter.
4
Print the matching log lines
Use Write-Output to print the variable $matchingLines.
PowerShell
Hint
Use Write-Output $matchingLines to display the filtered lines.
Practice
(1/5)
1. In PowerShell regex, what does the quantifier + mean when used like a+?
easy
A. Matches zero or more occurrences of 'a' in a row
B. Matches one or more occurrences of 'a' in a row
C. Matches exactly one 'a'
D. Matches zero or one occurrence of 'a'
Solution
Step 1: Understand the quantifier '+' meaning
The '+' quantifier means "one or more" repetitions of the preceding character or group.
Step 2: Apply to the example 'a+'
So, a+ matches strings with at least one 'a' repeated consecutively.
Final Answer:
Matches one or more occurrences of 'a' in a row -> Option B
Quick Check:
'+' means one or more repetitions [OK]
Hint: Remember '+' means at least one repetition [OK]
Common Mistakes:
Confusing '+' with '*' which means zero or more
Thinking '+' matches exactly one occurrence
Mixing '+' with '?' which means zero or one
2. Which of the following is the correct PowerShell regex pattern to match a string that starts with 'test' and ends immediately after?
easy
A. ^test$
B. test^$
C. ^test
D. test$
Solution
Step 1: Understand anchors '^' and '$'
'^' matches the start of the string, and '$' matches the end of the string.
Step 2: Combine anchors to match exact string
Using ^test$ ensures the string starts and ends exactly with 'test' and nothing else.
Final Answer:
^test$ -> Option A
Quick Check:
'^' start and '$' end anchors combined [OK]
Hint: Use ^ at start and $ at end for exact match [OK]
Common Mistakes:
Placing anchors in wrong order like 'test^$'
Using only one anchor and missing full match
Confusing anchors with quantifiers
3. What will be the output of this PowerShell command?
$text = 'aaaab'; $text -match '^a{3,5}b$'
medium
A. True
B. False
C. Error
D. Null
Solution
Step 1: Analyze the regex pattern '^a{3,5}b$'
This pattern matches strings starting with 3 to 5 'a's followed immediately by 'b' and nothing else.
Step 2: Check the string 'aaaab'
'aaaab' has 4 'a's followed by 'b', which fits the {3,5} quantifier and anchors.
Final Answer:
True -> Option A
Quick Check:
String matches pattern with 4 'a's and ends with 'b' [OK]
Hint: Count characters and check quantifier range [OK]
Common Mistakes:
Ignoring anchors and matching partial strings
Miscounting the number of 'a's
Confusing {3,5} with exact count
4. Identify the error in this PowerShell regex usage:
A. The pattern should use a single backslash: '^hello\d+$'
B. The quantifier + is invalid here
C. The pattern is correct and will match
D. The anchors ^ and $ are misplaced
Solution
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.
Step 2: Verify PowerShell string escaping
In PowerShell single-quoted strings, backslash is literal, so '\d' is correct to represent digit class.
Final Answer:
The pattern is correct and will match -> Option C
Quick Check:
Pattern syntax and anchors are correct [OK]
Hint: 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
5. You want to match strings in PowerShell that start with 'cat' followed by zero or more 's', and end exactly there. Which regex pattern should you use?
hard
A. ^cat?s$
B. ^cat+s$
C. cats*$
D. ^cats*$
Solution
Step 1: Understand the requirement
The string must start with 'cat', then have zero or more 's', and end immediately after.
Step 2: Analyze each pattern
^cats*$: ^cats*$ means start with 'cat', then zero or more 's', end of string -- matches requirement.
^cat+s$: ^cat+s$ means start with 'ca' then one or more 't's, then 's' at end -- incorrect.
^cat?s$: ^cat?s$ means start with 'ca', zero or one 't', then 's' at end -- incorrect.
cats*$: cats*$ no start anchor, so can match anywhere -- incorrect.
Final Answer:
^cats*$ -> Option D
Quick Check:
'^cat' start, 's*' zero or more, '$' end [OK]
Hint: Use ^ for start, $ for end, * for zero or more [OK]
Common Mistakes:
Using '+' instead of '*' for zero or more
Missing anchors causing partial matches
Misplacing quantifiers after wrong characters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.