正则表达式模式允许数字和星号

时间:2015-07-21 21:10:36

标签: regex powershell

如何获取$ MyBuildNumber中的最后4位数字,也可能是星号?我想$ NewVersion返回“2.8.1。*”;但是,这段代码不会为我返回任何内容:

$MyBuildNumber = "MyBuildNumberIs_2.8.1.*"
$VersionRegex = "\d+[*]?\.\d+[*]?\.\d+[*]?\.\d+[*]?"

$VersionData = [regex]::matches($MyBuildNumber,$VersionRegex)
switch($VersionData.Count)
{
   0        
      { 
         Write-Error "Could not find version number data in MyBuildNumber."
         exit 1
      }
   1 {}
}
$NewVersion = $VersionData[0]
Write-Host "Version: $NewVersion"

2 个答案:

答案 0 :(得分:6)

您可以使用带有数字的字符类和employee | key | value -------- |------ | ----- Jim | sales_tv | [{"yr": "2010", "loc": "us", "value": 4}, {"yr": "2011", "loc": "europe", "value": 18}, {"yr": "2012", "loc": "asia", "value": 28}] Jim | sales_tv | [{"yr": "2010", "loc": "us", "value": 10}, {"yr": "2011", "loc": "europe", "value": 5}, {"yr": "2012", "loc": "asia", "value": 40}] Jim | sales_radio | [{"yr": "2010", "loc": "us", "value": 11}, {"yr": "2011", "loc": "china", "value": 8}, {"yr": "2012", "loc": "us", "value": 76}]

*

答案 1 :(得分:2)

anubhava打败了我,但这是一个更紧凑的同一件事,因为为什么不:

([\d*]+\.){3}[\d*]+