2020-01-08 Powershell 正则匹配

$AliasName = "Zhao, Janice (KDC/HR)";

$matches = [System.Text.RegularExpressions.Regex]::Matches($AliasName, "(\w+)\,\s(\w+)\s");

$first = $matches[0].Groups[2].Value;

$last = $matches[0].Groups[1].Value;

表达式的括号是代表匹配数,如果不写括号,只会匹配到 Zhao, Janice



完整代码:

$AliasName = "Zhao, Janice (KDC/HR)";

$matches = [System.Text.RegularExpressions.Regex]::Matches($AliasName, "(\w+)\,\s(\w+)\s");

$first = $matches[0].Groups[2].Value;

$last = $matches[0].Groups[1].Value;

$outlookApp = New-Object -ComObject Outlook.Application;

$oGAL = $outlookApp.GetNamespace("MAPI").AddressLists("Global Address List")

$myAddrEntry = $oGAL.AddressEntries($AliasName)

$exchUser = $myAddrEntry.GetExchangeUser();

If([system.string]::IsNullOrEmpty($exchUser)){

    $GetAddress = "";

}

ElseIf($exchUser.FirstName -ne $first -or $exchUser.LastName -ne $last){

    $GetAddress = "";

}

Else{

    $GetAddress = $exchUser.PrimarySmtpAddress;

}

Write-Host $GetAddress;

你可能感兴趣的:(2020-01-08 Powershell 正则匹配)