使用 powershell,如何从(电子邮件的)主题行、正则表达式中提取 7 位数字?
Using powershell, how do I extract a 7-digit number from a subject-line (of an email ), regular expressions?
我有以下代码,它列出了(Outlook 的)收件箱文件夹中的前 5 个项目。
我如何只提取它的数字部分(比如 - 7 位任意数字,嵌入在其他文本中)?然后使用 Powershell 命令,我真的很想将这些提取的数字转储到 CSV 文件中(因此,它们可以很容易地合并到我使用的现有电子表格中)。
这是我的尝试:
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$sentMail.Items | select -last 10 TaskSubject # ideally, grabbing first 20
$matches2 = "\d+$"
$res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
但这并不 运行 正确,而是 .. 让我一直挂在等待输入符号上:就像这样:
>>
>>
>>
我是否需要在第 1 部分和第 2 部分之间创建一个单独的变量?
不确定 $matches
变量的用途,但请尝试用下面的内容替换最后一行。
对于主题行项目:
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' | % {([string]$_).Substring(0,12)} }
对于邮件正文项目:
$sentMail.Items | % { ($_.Body).Split("`n") | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' |% {([string]$_).Substring(0,12)} }
这里引用了我经常使用的 Select-String。
https://technet.microsoft.com/library/hh849903.aspx
这里是对 Phone 数字部分的引用,我从未使用过但觉得它很酷。
http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/24/use-powershell-to-search-a-group-of-files-for-phone-numbers.aspx
祝你好运!
这是通过主题行提取 7 位数字的编辑版本。这假设数字的每一侧都有一个 space,但如果需要可以稍微修改一下。您可能还想通过将 -First 部分更改为 Select * 或只是使范围更深 100 来调整深度。
$outlook = New-Object -com Outlook.Application
$Mail = $outlook.Session.GetDefaultFolder(6) # Folder Inbox
$Mail.Items | select -First 100 TaskSubject |
% { $_.TaskSubject | Select-String -Pattern '\s\d{7}\s'} |
% {((Select-String -InputObject $_ -Pattern '\s\d{7}\s').Line).split(" ") |
% {if(($_.Length -eq 7) -and ($_ -match '\d{7}')) {$_ | Out-File -FilePath "C:\Temp\SomeFile.csv" -Append}}}
其中一些 你已经解决/想通了,但我想用你当前的代码解释这些问题。
如果您期望多个匹配项并想要 return 那些,那么您需要使用 Select-String
和 -AllMatches
参数。在您的示例中,您的正则表达式当前正在主题末尾寻找一系列数字。那只会 return 一个匹配所以让我们看看你的代码的问题。
$sentMail.Items | select -last 10 TaskSubject
您正在过滤最后 10 个项目,但您没有存储它们供以后使用,因此它们只会显示在屏幕上。我们稍后会介绍解决方案。
使用 -match
的主要原因之一是获取 return 代码的布尔值,例如 if
块和 where
子句。您仍然可以按预期方式使用它。查看当前有问题的代码:
$res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
这两个大问题是您在每个项目上调用 Get-Content
(gc
)。 Get-Content
用于提取文件数据,而 $sentMail.Items
则不是。您还有一个 large where 块。 Where 块将根据 true 或 false 条件将数据传递到输出流。你的格式错误的语句 ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
不会这样做......至少不会很好。
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$matches2 = "\d+$"
$sentMail.Items | select -last 10 -ExpandProperty TaskSubject | ?{$_ -match $matches2} | %{$Matches[0]}
获取最后 10 个电子邮件主题并检查它们是否与正则表达式字符串 $matches2
匹配。 如果他们这样做,那么return字符串匹配到标准输出。
我有以下代码,它列出了(Outlook 的)收件箱文件夹中的前 5 个项目。
我如何只提取它的数字部分(比如 - 7 位任意数字,嵌入在其他文本中)?然后使用 Powershell 命令,我真的很想将这些提取的数字转储到 CSV 文件中(因此,它们可以很容易地合并到我使用的现有电子表格中)。
这是我的尝试:
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$sentMail.Items | select -last 10 TaskSubject # ideally, grabbing first 20
$matches2 = "\d+$"
$res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
但这并不 运行 正确,而是 .. 让我一直挂在等待输入符号上:就像这样:
>>
>>
>>
我是否需要在第 1 部分和第 2 部分之间创建一个单独的变量?
不确定 $matches
变量的用途,但请尝试用下面的内容替换最后一行。
对于主题行项目:
$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' | % {([string]$_).Substring(0,12)} }
对于邮件正文项目:
$sentMail.Items | % { ($_.Body).Split("`n") | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' |% {([string]$_).Substring(0,12)} }
这里引用了我经常使用的 Select-String。 https://technet.microsoft.com/library/hh849903.aspx
这里是对 Phone 数字部分的引用,我从未使用过但觉得它很酷。 http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/24/use-powershell-to-search-a-group-of-files-for-phone-numbers.aspx
祝你好运!
这是通过主题行提取 7 位数字的编辑版本。这假设数字的每一侧都有一个 space,但如果需要可以稍微修改一下。您可能还想通过将 -First 部分更改为 Select * 或只是使范围更深 100 来调整深度。
$outlook = New-Object -com Outlook.Application
$Mail = $outlook.Session.GetDefaultFolder(6) # Folder Inbox
$Mail.Items | select -First 100 TaskSubject |
% { $_.TaskSubject | Select-String -Pattern '\s\d{7}\s'} |
% {((Select-String -InputObject $_ -Pattern '\s\d{7}\s').Line).split(" ") |
% {if(($_.Length -eq 7) -and ($_ -match '\d{7}')) {$_ | Out-File -FilePath "C:\Temp\SomeFile.csv" -Append}}}
其中一些 你已经解决/想通了,但我想用你当前的代码解释这些问题。
如果您期望多个匹配项并想要 return 那些,那么您需要使用 Select-String
和 -AllMatches
参数。在您的示例中,您的正则表达式当前正在主题末尾寻找一系列数字。那只会 return 一个匹配所以让我们看看你的代码的问题。
$sentMail.Items | select -last 10 TaskSubject
您正在过滤最后 10 个项目,但您没有存储它们供以后使用,因此它们只会显示在屏幕上。我们稍后会介绍解决方案。
使用 -match
的主要原因之一是获取 return 代码的布尔值,例如 if
块和 where
子句。您仍然可以按预期方式使用它。查看当前有问题的代码:
$res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
这两个大问题是您在每个项目上调用 Get-Content
(gc
)。 Get-Content
用于提取文件数据,而 $sentMail.Items
则不是。您还有一个 large where 块。 Where 块将根据 true 或 false 条件将数据传递到输出流。你的格式错误的语句 ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }
不会这样做......至少不会很好。
$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$matches2 = "\d+$"
$sentMail.Items | select -last 10 -ExpandProperty TaskSubject | ?{$_ -match $matches2} | %{$Matches[0]}
获取最后 10 个电子邮件主题并检查它们是否与正则表达式字符串 $matches2
匹配。 如果他们这样做,那么return字符串匹配到标准输出。