【发布时间】:2019-10-10 07:23:03
【问题描述】:
我有一个如下所示的纯文本文件:
"sample1@gmail.com"
"sample2.test@gmail.com"
"sample3.test.test2@gmail.com"
等等……
现在使用 Powershell 我正在尝试逐行读取此纯文本文件并读取不带双引号的电子邮件并将其添加到数组列表中:
$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
}
我不知道为什么,但在执行上述代码块后,我收到带有双引号的电子邮件,这是输出:
email: "sample1@gmail.com"
email: "sample2.test@gmail.com"
email: "sample3.test.test2@gmail.com"
等等……
但我想要以下内容(不带双引号的电子邮件):
email: sample1@gmail.com
email: sample2.test@gmail.com
email: sample3.test.test2@gmail.com
似乎正则表达式被带上了双引号......
【问题讨论】:
-
如果您在每一行上使用
.Trim('"'),您将获得 没有 修剪字符串中的前导/尾随字符的行。在这种情况下,它将删除前导和尾随双引号。 [咧嘴]
标签: regex powershell powershell-2.0 regex-greedy