【问题标题】:PowerShell RegEx pattern matching for Mobile / Cell phone in AD?AD中移动/手机的PowerShell RegEx模式匹配?
【发布时间】:2019-04-30 08:20:04
【问题描述】:

与以下模式匹配的 Powershell RegEx 模式是什么?

+31 123456789
0123456789
0123 456 789
0 123 456 789

我需要将其合并到:

If ($MobilePhone.ToString() -match '0(?<CountryCode>\d)\s+(?<Number>.........)')
{ "+31 $($Matches['CountryCode']) $($Matches['Number'])" }
Else
{ $MobilePhone.ToString() }

【问题讨论】:

  • 为什么这个问题会被扣分或被扣分?
  • 我没有在这里投票,但我想人们认为付出的努力太少了,特别是因为你是一名高级系统工程师。 (您在下面向 Toby 添加了一个后续问题,这似乎也表明之前没有工作 - 读者通常会尝试鼓励您先用尽自己的选项)。

标签: regex powershell scripting


【解决方案1】:

我认为这可能会对您有所帮助:

$regex = "^([\+|0-9 ][ 0-9.]{1,12})$"
$number = "+31 123456789"

if($number -match $regex){
    Write-Host "It matches" -ForegroundColor Green
}else{
    Write-Host "Invalid number" -ForegroundColor Red
}

【讨论】:

    【解决方案2】:

    给你

    "+31 123456789" -match '^\+\d{2}\s\d{9}$'
    "0123456789" -match '^\d{10}$'
    "0123 456 789" -match '^\d{4}(\s\d+){2}$'
    "0 123 456 789" -match '^\d(\s\d{3}){3}$'
    

    相关:Modifying PowerShell to display phone number in International format without changing the AD Attributes?

    【讨论】:

    猜你喜欢
    • 2011-06-20
    • 2018-05-29
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多