0
0
PowerShellscripting~30 mins

Why regex enables pattern matching in PowerShell - See It in Action

Choose your learning style9 modes available
Why regex enables pattern matching
📖 Scenario: You work in IT support and often need to find specific patterns in text logs to quickly spot errors or important messages.
🎯 Goal: Build a simple PowerShell script that uses regex to find all email addresses in a list of text lines.
📋 What You'll Learn
Create a list of text lines containing some email addresses
Create a regex pattern variable to match email addresses
Use a loop to find all matches in the list using the regex pattern
Print each found email address
💡 Why This Matters
🌍 Real World
IT support and system admins often scan logs or messages to find specific patterns like email addresses or error codes quickly.
💼 Career
Knowing regex and scripting helps automate searching and filtering tasks, saving time and reducing errors in daily work.
Progress0 / 4 steps
1
Create the list of text lines
Create a variable called lines and assign it an array with these exact strings: 'Contact us at support@example.com', 'Send feedback to feedback@domain.org', 'No email here', 'Reach admin@company.net anytime'.
PowerShell
Need a hint?

Use @( ... ) to create an array in PowerShell.

2
Create the regex pattern variable
Create a variable called emailPattern and assign it the regex string '\b[\w.-]+@[\w.-]+\.\w{2,}\b' to match email addresses.
PowerShell
Need a hint?

Use double backslashes \\ in the string to escape backslashes in PowerShell.

3
Find all email addresses using regex
Use a foreach loop with variable line to go through $lines. Inside the loop, use [regex]::Matches($line, $emailPattern) to find matches. For each match, add the matched value to a list called foundEmails. Initialize foundEmails as an empty list before the loop.
PowerShell
Need a hint?

Use @() to create an empty array. Use nested loops to collect matches.

4
Print all found email addresses
Use foreach with variable email to go through $foundEmails and print each email using Write-Output.
PowerShell
Need a hint?

Use Write-Output inside a loop to print each email.