Regex 我正在尝试在powershell上为加拿大地址编写正则表达式

Regex 我正在尝试在powershell上为加拿大地址编写正则表达式,regex,powershell,street-address,Regex,Powershell,Street Address,这是地址方法 数字可能是12或412,芬奇大道东有多少个单词 所以我试试这个 ^[0-9]+\s+[a-zA-Z]+\s+[a-zA-Z]+\s+[a-zA-Z]+[,]{1}+\s[a-zA-Z]+[,]{1}+\s+[a-zA-Z]+[,]{1}+\s[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$ 我通常建议使用正则表达式捕获组,这样您就可以将匹配问题分解并简化为更小的集合。在大多数情况下,我使用\d和\w,s来匹配数字、标准字母和空格 我通常在将其放入代码之前进

这是地址方法

数字可能是12或412,芬奇大道东有多少个单词

所以我试试这个

^[0-9]+\s+[a-zA-Z]+\s+[a-zA-Z]+\s+[a-zA-Z]+[,]{1}+\s[a-zA-Z]+[,]{1}+\s+[a-zA-Z]+[,]{1}+\s[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$

我通常建议使用正则表达式捕获组,这样您就可以将匹配问题分解并简化为更小的集合。在大多数情况下,我使用\d和\w,s来匹配数字、标准字母和空格

我通常在将其放入代码之前进行实验,因为它提供了一种很好的交互方式来处理表达式和示例

关于你的问题,我想表达的是:

$regexp = "^(\d+)\s*((\w+\s*)+),\s*(\w+),\s*(\w+),\s*((\w\d)*)$"
在PowerShell中,我喜欢使用direct正则表达式类,因为它提供了比标准匹配运算符更高的粒度

# Example match and results
$sample = "1460 Finch Ave East, Toronto, Ontario, A1A1A1"
$match = [regex]::Match($sample, $regexp)
$match.Success
$match | Select -ExpandProperty groups | Format-Table Name, Value

# Constructed fields
@{
    number = $match.Groups[1]
    street = $match.Groups[2]
    city = $match.Groups[4]
    state = $match.Groups[5]
    areacode = $match.Groups[6]
}
因此,这将导致$match.Success$true,以下编号的捕获组将显示在组列表中:

对于构建字段,可以忽略3和7,因为它们是部分组:

Name     Value
----     -----
areacode A1A1A1
street   Finch Ave East
city     Toronto
state    Ontario
number   1460

我通常建议使用正则表达式捕获组,这样您就可以将匹配问题分解并简化为更小的集合。在大多数情况下,我使用\d和\w,s来匹配数字、标准字母和空格

我通常在将其放入代码之前进行实验,因为它提供了一种很好的交互方式来处理表达式和示例

关于你的问题,我想表达的是:

$regexp = "^(\d+)\s*((\w+\s*)+),\s*(\w+),\s*(\w+),\s*((\w\d)*)$"
在PowerShell中,我喜欢使用direct正则表达式类,因为它提供了比标准匹配运算符更高的粒度

# Example match and results
$sample = "1460 Finch Ave East, Toronto, Ontario, A1A1A1"
$match = [regex]::Match($sample, $regexp)
$match.Success
$match | Select -ExpandProperty groups | Format-Table Name, Value

# Constructed fields
@{
    number = $match.Groups[1]
    street = $match.Groups[2]
    city = $match.Groups[4]
    state = $match.Groups[5]
    areacode = $match.Groups[6]
}
因此,这将导致$match.Success$true,以下编号的捕获组将显示在组列表中:

对于构建字段,可以忽略3和7,因为它们是部分组:

Name     Value
----     -----
areacode A1A1A1
street   Finch Ave East
city     Toronto
state    Ontario
number   1460
要添加到,我建议使用命名的捕获组和$Matches自动变量。这使得抓取单个字段并将其转换为多个输入字符串的对象变得非常容易:

function Split-CanadianAddress {
  param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [string[]]$InputString
  )

  $Pattern = "^(?<Number>\d+)\s*(?<Street>(\w+\s*)+),\s*(?<City>(\w+\s*)+),\s*(?<State>(\w+\s*)+),\s*(?<AreaCode>(\w\d)*)$"

  foreach($String in $InputString){
    if($String -match $Pattern){
      $Fields = @{}
      $Matches.Keys |Where-Object {$_ -isnot [int]} |ForEach-Object {
        $Fields.Add($_,$Matches[$_])
      }
      [pscustomobject]$Fields
    }
  }
}
我已经更新了模式,以允许城市和州名称中的空格以及不列颠哥伦比亚省新威斯敏斯特市的空格。我建议使用命名捕获组和$Matches自动变量。这使得抓取单个字段并将其转换为多个输入字符串的对象变得非常容易:

function Split-CanadianAddress {
  param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [string[]]$InputString
  )

  $Pattern = "^(?<Number>\d+)\s*(?<Street>(\w+\s*)+),\s*(?<City>(\w+\s*)+),\s*(?<State>(\w+\s*)+),\s*(?<AreaCode>(\w\d)*)$"

  foreach($String in $InputString){
    if($String -match $Pattern){
      $Fields = @{}
      $Matches.Keys |Where-Object {$_ -isnot [int]} |ForEach-Object {
        $Fields.Add($_,$Matches[$_])
      }
      [pscustomobject]$Fields
    }
  }
}
我已经更新了模式,以允许城市和州名称中的空间,以及不列颠哥伦比亚省的新威斯敏斯特