Search code examples
regexpowershellpowershell-2.0regex-greedy

Get text between two characters


I have a plain text file like below:

"sample1@gmail.com"
"sample2.test@gmail.com"
"sample3.test.test2@gmail.com"

and so on...

Now using Powershell I am trying to read line by line this plain text file and read the email without double quotes and add it to an array list:

$arrayListEmails = New-Object System.Collections.ArrayList

$regex = '"([^/)]+)"'
[System.IO.File]::ReadLines("C:\temp\emailsList.txt") | Where-Object {$_ -match $regex} | ForEach-Object {
    write-host "email: $_"
    $arrayListEmails.Add($_) > $null
}

I don't know why but after executing above block of code I get email with double quotes, this is the output:

email: "sample1@gmail.com"
email: "sample2.test@gmail.com"
email: "sample3.test.test2@gmail.com"

and so on...

But I want the following (emails without double quotes):

email: sample1@gmail.com
email: sample2.test@gmail.com
email: sample3.test.test2@gmail.com

It seems like regex is taken the double quotes....


Solution

  • Else you can do it (import-csv remove double-quote on columns):

    $Yourlist=import-csv "C:\temp\emailsList.txt" -Header Email
    $Yourlist | %{ "email : {0}" -f $_.Email }