bash regex 匹配语义版本号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24318927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:43:20  来源:igfitidea点击:

bash regex to match semantic version number

regexbashsemantic-versioning

提问by lukemh

I have the following:

我有以下几点:

versionNumber=$(sw_vers -productVersion) # Finds version number

versionShort=${versionNumber:0:4}  # Cut string to 1 decimal place for calculation

which works when versions are like this:

当版本是这样的时它的工作原理:

10.9.2
10.9.5

but it will not match

但它不会匹配

10.10.3

as it will return only

因为它只会返回

10.1

but I want the versionShort to be set to

但我希望将 versionShort 设置为

10.10

I am wanting to match the major version, the first dot and the minor version as above.

我想匹配上面的主要版本,第一个点和次要版本。

回答by Amadan

Regexpless solution - cut off last dot and whatever follows it:

Regexpless 解决方案 - 切断最后一个点和它后面的任何内容:

versionShort=${versionNumber%.*}

回答by n8felton

I had a similar question, but I needed access to all 3 segments. I did a bit of research and testing and I found this to work well

我有一个类似的问题,但我需要访问所有 3 个部分。我做了一些研究和测试,我发现这很好用

product_version=$(sw_vers -productVersion)
semver=( ${product_version//./ } )
major="${semver[0]}"
minor="${semver[1]}"
patch="${semver[2]}"
echo "${major}.${minor}.${patch}"

To answer this question directly, you could

要直接回答这个问题,您可以

product_version=$(sw_vers -productVersion)
semver=( ${product_version//./ } )
major="${semver[0]}"
minor="${semver[1]}"
patch="${semver[2]}"
versionShort="${major}.${minor}"

or you can use less variables

或者你可以使用更少的变量

product_version=$(sw_vers -productVersion)
semver=( ${product_version//./ } )
versionShort="${semver[0]}.${semver[1]}"

回答by Aleks-Daniel Jakimenko-A.

Regexp solution:

正则表达式解决方案:

[[ $versionNumber =~ ^[0-9]+\.[0-9]+ ]] && echo "${BASH_REMATCH[0]}"

It will always print first two numbers, for example all these:

它将始终打印前两个数字,例如所有这些:

10.5
10.5.9
10.5.8.2

Will result in 10.5output. You can also add an elseclause to check if something wrong happened (no match found).

将导致10.5输出。您还可以添加一个else子句来检查是否发生了错误(未找到匹配项)。

Here is a longer version:

这是一个更长的版本:

if [[ $versionNumber =~ ^[0-9]+\.[0-9]+ ]]; then
    versionShort=${BASH_REMATCH[0]}
else
    echo "Something is wrong with your version" >&2
fi

回答by rhinoceros.xn

https://github.com/fsaintjacques/semver-toolhttps://github.com/fsaintjacques/semver-tool/blob/master/src/semver

https://github.com/fsaintjacques/semver-tool https://github.com/fsaintjacques/semver-tool/blob/master/src/semver

SEMVER_REGEX="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$"


function validate-version {
  local version=
  if [[ "$version" =~ $SEMVER_REGEX ]]; then
    # if a second argument is passed, store the result in var named by 
    if [ "$#" -eq "2" ]; then
      local major=${BASH_REMATCH[1]}
      local minor=${BASH_REMATCH[2]}
      local patch=${BASH_REMATCH[3]}
      local prere=${BASH_REMATCH[4]}
      local build=${BASH_REMATCH[5]}
      eval "=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
    else
      echo "$version"
    fi
  else
    error "version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. See help for more information."
  fi
}