Find all mail address in a file using regex

powershell, regex

Solution

`-match` returns strings with the content, so it works better with a string-array where it can find a match per line. What you want is a "global" search I believe it's called. In PowerShell you can do that using `Select-String` with the `-AllMatches` parameter.

Try the following:

(Select-String -InputObject $myString -Pattern '\w+@\w+\.\w+' -AllMatches).Matches

Example:

$myString = @"
user@domain.no hhaksda user@domain.com
dsajklg user@domain.net
"@

PS > (Select-String -InputObject $myString -Pattern '\w+@\w+\.\w+' -AllMatches).Matches | ft * -AutoSize

Groups            Success Captures          Index Length Value
------            ------- --------          ----- ------ -----          
{user@domain.no}     True {user@domain.no}      0     14 user@domain.no 
{user@domain.com}    True {user@domain.com}    23     15 user@domain.com
{user@domain.net}    True {user@domain.net}    48     15 user@domain.net

Problem

I need to read with powershell a lot of files and getting all email address. I tried this solution ``` $myString -match '\w+@\w+\.\w+' ``` The problem is that the variable `$matches` contains only the first match. Am I missing something?

Original source

Related problems