正则表达式从表中检索值

时间:2016-08-09 11:11:24

标签: regex string powershell

我们让用户回复电子邮件,我们需要检索表中提到的日期。

目前我为我们制作了以下代码:

$temp = ($Mail.Body.Text -creplace '(?m)^\s*\r?\n' -split "User name`r`n`tLogon name`r`n`tEnd date`r`n`tNew end date")[1]
(($temp -split "`r`n`t")[3] -split "`r`n")[0]

正如您所看到的,它很长并且非常依赖-split。有没有更简单的方法来检索日期?使用Select-StringRegex或其他内容?

电子邮件中的表格格式始终相同,因为用户需要在HTML表格中填写日期。所以我们需要能够读取该表。在下面的示例中,结果为01/08/2016

此类电子邮件的示例:

Date added below



From: GBR Service Desk 
Sent: Wendesday 29 juni 2016 7:00
To: Bond, James (London) GBR
Subject: REMINDER Expiring user: 
Importance: High



Dear James

This is a reminder e-mail to inform you that the following Windows user account will expire soon:

User name

    Logon name

    End date

    New end date


Smith, Jobn (Manchester) GBR

    jent

    01/07/2016

    01/08/2016



Because you are registered as the manager of this user, we would like to ask you to verify if this account is still nee
ded after its end date. More text...

Yours sincerely

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用Select-String查找类似日期的模式,然后选择最后一个模式:

$EndDate = ($Mail.Body.Text |Select-String '\d{2}/\d{2}/\d{4}' -AllMatches).Matches[-1].Value