0
0
PowerShellscripting~15 mins

Common regex patterns in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Common regex patterns
📖 Scenario: You work in IT support and often need to check if user inputs match common patterns like email addresses, phone numbers, or postal codes.
🎯 Goal: Build a PowerShell script that uses common regex patterns to find matches in given text samples.
📋 What You'll Learn
Create variables with sample text strings
Define regex pattern variables for email, phone number, and postal code
Use the -match operator with the regex patterns to check for matches
Print the results clearly showing which pattern matched
💡 Why This Matters
🌍 Real World
IT support and automation scripts often need to validate user input like emails or phone numbers to ensure data quality.
💼 Career
Knowing common regex patterns and how to apply them in scripts is useful for system administrators, support engineers, and automation specialists.
Progress0 / 4 steps
1
Create sample text variables
Create three variables called $emailSample, $phoneSample, and $postalSample with these exact string values: 'contact@example.com', '123-456-7890', and '90210' respectively.
PowerShell
Need a hint?

Use = to assign the exact strings to the variables.

2
Define regex pattern variables
Create three variables called $emailPattern, $phonePattern, and $postalPattern with these exact regex strings: '^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$' for email, '^\d{3}-\d{3}-\d{4}$' for phone number, and '^\d{5}$' for postal code.
PowerShell
Need a hint?

Use single quotes and escape backslashes properly in regex strings.

3
Check matches using regex patterns
Use the -match operator with variables $emailSample, $phoneSample, and $postalSample against $emailPattern, $phonePattern, and $postalPattern respectively. Store the boolean results in variables $emailMatch, $phoneMatch, and $postalMatch.
PowerShell
Need a hint?

Use -match to test if the sample matches the pattern and assign the result to the match variables.

4
Print the match results
Print the values of $emailMatch, $phoneMatch, and $postalMatch each on a new line using Write-Output.
PowerShell
Need a hint?

Use Write-Output to print each match result on its own line.