分离返回和聊天GCP理解

huangapple go评论53阅读模式
英文:

Split returns and chat GCP understanding

问题

$FilePath = 'c:\temp\test.csv'

# 在脚本中使用文件路径
$Entry = Get-Content -Path $FilePath -raw
$Entry = $Entry -split "\r?\n"

# 初始化 $Result 数组
$Result = @()

# 遍历数组中的每个电子邮件地址
foreach ($Email in $Entry) {
    $ValidEmail = $null
    $Value = $null
    # 去除前导和尾随空格,并将中间空格替换为点号
    $Value = $Email.Trim() -replace '\s+', '.'

    try {
        # 尝试创建一个新的邮件地址对象以验证地址
        $ValidEmail = (New-Object System.Net.Mail.MailAddress $Value).Address
    }
    catch {
        # 如果出现异常,请忽略它并继续下一个验证方法
        # 尝试从显示名称格式中提取电子邮件地址
        if ($Value -match '(?<!<)(?<Email>\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b)(?!>)') {
            $ValidEmail = $matches['Email']
        }
        else {
            # 如果其他方法都失败,尝试通过剥离和添加来形成有效地址
            $ValidEmail = ($Value -replace '[<>,()@,]', '' -replace '\s+', '.' -replace '[^A-Za-z0-9._%+-]+', '') + '@Domain.com'
        }
    }

    # 将有效电子邮件地址添加到 $Result 数组
    $Result += $ValidEmail
}

# 返回有效的电子邮件地址
return $Result

当我运行上述代码在VSCode中,并在行$Entry = $Entry -split "\r?\n" 处暂停时,答案是正确的。

如果我关闭VSCode,重新打开它并运行脚本而不进行调试,则答案是错误的。

测试数据

Mark Gonzalez
Dan.Ca@domain.com
Jennifer Zal <jennifer.zal@domain.com>

调试答案

Mark.Gonzalez@domain.com
Dan.Ca@domain.com
Jennifer.Zal@domain.com

仅运行脚本

Mark.Gonzalez.Dan.Ca@domain.com.Jennifer.Zal
英文:

Code

$FilePath = &#39;c:\temp\test.csv&#39;
            
# Use the file path in your script
$Entry = Get-Content -Path $FilePath -raw
$Entry = $Entry -split &quot;\r?\n&quot;
    
# Initialize the $Result array
$Result = @()

# Loop through each email address in the array
foreach ($Email in $Entry) {
    $ValidEmail = $null
    $Value = $null
    # Remove leading and trailing spaces, and replace middle spaces with dots
    $Value = $Email.Trim() -replace &#39;\s+&#39;, &#39;.&#39;

    try {
        # Try to create a new mail address object to validate the address
        $ValidEmail = (New-Object System.Net.Mail.MailAddress $Value).Address
    }
    catch {
        # If there is an exception, ignore it and move on to the next validation method
        # Try to extract the email address from a display name format
        if ($Value -match &#39;(?&lt;!&lt;)(?&lt;Email&gt;\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b)(?!&gt;)&#39;) {
            $ValidEmail = $matches[&#39;Email&#39;]
        }
        else {
            # If all else fails, try to form a valid address by stripping and adding
            $ValidEmail = ($Value -replace &#39;[&lt;&gt;,()@,]&#39;, &#39;&#39; -replace &#39;\s+&#39;, &#39;.&#39; -replace &#39;[^A-Za-z0-9._%+-]+&#39;, &#39;&#39;) + &#39;@Domain.com&#39;
        }
    }

    # Add the valid email address to the $Result array
    $Result += $ValidEmail
}
    
# Return the valid email addresses
return $Result

When I run above in VSCode and pause on the line $Entry = $Entry -split &quot;\r?\n&quot;
The answer is correct

If I close vscode, open it and run the script with no debug the answer is wrong.

Test data

Mark Gonzalez
Dan.Ca@domain.com
Jennifer Zal &lt;jennifer.zal@domain.com&gt;

Debug answer

Mark.Gonzalez@domain.com
Dan.Ca@domain.com
Jennifer.Zald@domain.com

Script just run

Mark.Gonzalez.Dan.Ca@domain.com.Jennifer.Zal

答案1

得分: 2

* 对于你的症状,没有明显的解释;总的来说,在使用 Visual Studio Code 中的 **PIC (PowerShell Integrated Console)** 时一个值得注意的陷阱是,**_状态可能会在运行之间保留_,可能会影响后续的运行** - **_除非_你配置 PIC 以在每次调试运行时启动一个 _新的临时会话_** - 请参考[此答案](https://stackoverflow.com/a/71970578/45375)。


* 我建议按照以下方式简化你的代码,这不仅提高了效率,还可能解决问题:


$FilePath = 'c:\temp\test.csv';

这会直接输出修改后的行。

Get-Content $FilePath |
ForEach-Object {
$addr = $_ -replace '^.<|>.$';
try {
([System.Net.Mail.MailAddress] $addr).Address
} catch {
$addr.Trim() -replace '[a-z0-9._%+-]' -replace '\s+', '.';
}
}

英文:

<!-- language-all: sh -->

  • There's no obvious explanation for your symptom; on a general note, a notable pitfall when using the PIC (PowerShell Integrated Console) in Visual Studio Code is that state can linger between runs, potentially affecting subsequent runs - unless you configure the PIC to start a new, temporary session for each debugging run - see this answer.

  • I suggest streamlining your code as follows, which not only improves its efficiency, but may make the problem go away:

$FilePath = &#39;c:\temp\test.csv&#39;

# This outputs the modified lines directly.
Get-Content $FilePath |
  ForEach-Object {
    $addr = $_ -replace &#39;^.*&lt;|&gt;.*$&#39;
    try {
      ([System.Net.Mail.MailAddress] $addr).Address
    } catch {
      $addr.Trim() -replace &#39;[^a-z0-9._%+-]&#39; -replace &#39;\s+&#39;, &#39;.&#39;
    }
  }

huangapple
  • 本文由 发表于 2023年3月10日 01:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定