Introduction
Regular expressions help you find or check text patterns quickly and easily.
Select-String -Pattern '<regex_pattern>' -Path '<file_path>' # Or use -match operator: $string -match '<regex_pattern>'
'hello123' -match '\d+'
'test@example.com' -match '^[\w.-]+@[\w.-]+\.\w{2,}$'
Select-String -Pattern '\b\d{3}-\d{2}-\d{4}\b' -Path 'data.txt'
$text = 'Contact me at user@example.com or call 123-456-7890.' if ($text -match '[\w.-]+@[\w.-]+\.\w{2,}') { Write-Output "Found an email address: $($matches[0])" } if ($text -match '\d{3}-\d{3}-\d{4}') { Write-Output "Found a phone number: $($matches[0])" }
$text = 'User123'
if ($text -match '\w{4}\d{3}') { 'Match' } else { 'No Match' }$email = 'test@example.com'
if ($email -match '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2}') { 'Valid' } else { 'Invalid' }Pass the test and this pattern moves unknown