Reputation: 10650
I have a plain text file like below:
"[email protected]"
"[email protected]"
"[email protected]"
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: "[email protected]"
email: "[email protected]"
email: "[email protected]"
and so on...
But I want the following (emails without double quotes):
email: [email protected]
email: [email protected]
email: [email protected]
It seems like regex is taken the double quotes....
Upvotes: 0
Views: 86
Reputation: 7489
there is a remarkably simple way to clean away leading and trailing characters from a string. use the .Trim()
string method. [grin] it will remove each character in the trim list from the ends of the target string.
# fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
"[email protected]"
"[email protected]"
"[email protected]"
'@ -split [System.Environment]::NewLine
$DeQuotedEmailList = foreach ($IS_Item in $InStuff)
{
# the trim string is <single><double><single> quotes
# it will remove any leading and/or trailing double quotes
$IS_Item.Trim('"')
}
$DeQuotedEmailList
output ...
[email protected]
[email protected]
[email protected]
Upvotes: 0
Reputation: 196
Maybe this regex could help you:
"([^/\)]+?)"
You have to use the group $1
to get the value that you want. That's the email without quotes.
Upvotes: 0
Reputation: 17472
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 }
Upvotes: 2
Reputation: 17472
Because you emails have quote :) Try this
write-host ("email: " + $_.Replace('"', ""))
$arrayListEmails.Add($_.Replace('"', "")) > $null
Upvotes: 0