我们如何检查字符串是否是版本号 [英] How can we check if string is a version number or not

查看:27
本文介绍了我们如何检查字符串是否是版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在shell脚本中检查字符串是否包含数字/十进制格式的版本

How to check if a string contains version in numberic/decimal format in shell script

例如我们有 1.2.3.5 或 2.3.5

for eg we have 1.2.3.5 or 2.3.5

如果我们对这里的字符数没有限制怎么办.它也可以是 x.x.x.x 或 x.x.

What if we do not have a constraint on the number of characters we have in here. It could x.x.x.x or x.x as well.

推荐答案

使用 bash 正则表达式:

echo -n "Test: "
read i

if [[ $i =~ ^[0-9]+(\.[0-9]+){2,3}$ ]]; 
then
  echo Yes
fi

这接受 digits.digits.digitsdigits.digits.digits.digits

更改 {2,3} 以缩小或放大可接受的 .digits 数量(或 {2,} 表示至少2")

Change {2,3} to shrink or enlarge the acceptable number of .digits (or {2,} for "at least 2")

  • ^ 表示字符串的开头
  • [0-9]+ 表示至少一位数
  • \. 是一个点
  • (...){2,3} 接受 ()
  • 中的 2 或 3 个内容
  • $ 表示字符串结束
  • ^ means beginning of string
  • [0-9]+ means at least one digit
  • \. is a dot
  • (...){2,3} accepts 2 or 3 of what's inside the ()
  • $ means end of string

这篇关于我们如何检查字符串是否是版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆