在 Powershell 中,如何仅从电子邮件主题行中提取 7 位数字?
In Powershell, how to I extract only the 7-digit numbers from email subject-lines?
这是我的 Powershell 代码:
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6)
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '\d{7}' }
所以这将打印所有包含 7 位字符串的电子邮件主题行。但是,我只想 在这些行中打印 7 位数字的字符串。
我应该使用 match
,我相信。但是匹配 returns true/false ?解决方法是什么?谢谢
我正在回答自己,因为我只是想出了一个方法:
我们需要另一个管道 ( |
) ,在这里我们使用 match
命令:像这样 -
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '\d{7}'
| % {([string]$_) -match '\d{7}' | out-null; $matches) }
打印出来:
Name Value
----- -----
0 8293798
0 8233798
0 1223798
..etc etc..
但这是 2 列,我只想要 1 列
试试这个
$sentMail.Items | % { $RESULT=[Regex]::Match($_.TaskSubject ,"\d{7}"); if($RESULT.Success){$RESULT.Value} }
这是我的 Powershell 代码:
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6)
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '\d{7}' }
所以这将打印所有包含 7 位字符串的电子邮件主题行。但是,我只想 在这些行中打印 7 位数字的字符串。
我应该使用 match
,我相信。但是匹配 returns true/false ?解决方法是什么?谢谢
我正在回答自己,因为我只是想出了一个方法:
我们需要另一个管道 ( |
) ,在这里我们使用 match
命令:像这样 -
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '\d{7}'
| % {([string]$_) -match '\d{7}' | out-null; $matches) }
打印出来:
Name Value
----- -----
0 8293798
0 8233798
0 1223798
..etc etc..
但这是 2 列,我只想要 1 列
试试这个
$sentMail.Items | % { $RESULT=[Regex]::Match($_.TaskSubject ,"\d{7}"); if($RESULT.Success){$RESULT.Value} }