Finding multiple RegEx matches

by May 20, 2011

In a previous tip, you learned that Select-Object can find multiple matches. Here is a function called matches. You can submit a regular expressions pattern and all text piped into matches will be matched:

$pattern = '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b'
$text = "My email is tobias.weltner@email.de and also tobias@powershell.de"

filter Matches($pattern) {
$_ | Select-String -AllMatches $pattern | 
  Select-Object -ExpandProperty Matches | 
  Select-Object -ExpandProperty Value
}

$text | Matches $pattern

 

Twitter This Tip!
ReTweet this Tip!