我正在尝试让Powershell使用Regex验证电子邮件地址,并将电子邮件地址放入好的和坏的csv文件中.我可以让它跳过一行并写入文件,但无法让它定位电子邮件地址并验证它们,然后将行写入好的和坏的文件.我可以在C#和JavaScript中完成它,但从未在Powershell中完成它.我知道这可以做到,但不知道如何做.
这是我到目前为止:
Function IsValidEmail {
Param ([string] $In)
# Returns true if In is in valid e-mail format.
[system.Text.RegularExpressions.Regex]::IsMatch($In,
"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|
(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
## Now we need to check the original file for invalid and valid emails.**
$list = Get-Content C:\Emails\OriginalEmails\emailAddresses.csv
# This way we also use the foreach loop.
##======= Test to see if the file exists ===========
if (!(Test-Path "C:\Emails\ValidEmails\ValidEmails.csv")) {
New-Item -path C:\Emails\ValidEmails -name ValidEmails.csv -type
"file" # -value "my new text"
Write-Host "Created new file and text …Run Code Online (Sandbox Code Playgroud)