检查字符串是否与Bash脚本中的regex匹配

时间:2021-09-30 11:29:21

One of the arguments that my script receives is a date in the following format: yyyymmdd.

我的脚本收到的一个参数是以下格式的日期:yyyyymmdd。

I want to check if I get a valid date as an input.

我想检查我是否有一个有效的日期作为输入。

How can I do this? I am trying to use a regex like: [0-9]\{\8}

我该怎么做呢?我正试着使用一种正则表达式:[0-9]\{\8}

3 个解决方案

#1


167  

You can say:

你可以说:

[[ $date =~ ^[0-9]{8}$ ]] && echo "yes"

Or more accurate:

或者更准确:

[[ $date =~ ^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$ ]] && echo "yes"
#           |^^^^^^^^ ^^^^^^ ^^^^^^  ^^^^^^ ^^^^^^^^^^ ^^^^^^ |
#           |   |     ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^ |
#           |   |          |                   |              |
#           |   |           \                  |              |
#           | --year--   --month--           --day--          |
#           |          either 01...09      either 01..09     end of line
# start of line            or 10,11,12         or 10..29
#                                              or 30, 31

That is, you can define a regex in bash matching the format you want. This way you can do:

也就是说,您可以在bash中定义一个regex,以匹配所需的格式。这样你就可以做到:

[[ $date =~ ^regex$ ]] && echo "matched" || echo "did not match"

Note this is based on the solution by Aleks-Daniel Jakimenko in User input date format verification in bash.

注意,这是基于Aleks-Daniel Jakimenko在bash中的用户输入日期格式验证的解决方案。


In shells such as sh or fish – less equipped then bash – you can use grep:

在像sh或fish这样的贝壳中——没有装备那么bash——你可以使用grep:

(echo "$date" | grep -Eq  ^regex$) && echo "matched" || echo "did not match"

#2


28  

In bash version 3 you can use the '=~' operator:

在bash版本3中,您可以使用'=~'操作符:

if [[ "$date" =~ "[0-9]\{8\}" ]]; then
    echo "Valid date"
else
    echo "Invalid date"
fi

Reference: http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF

参考:http://tldp.org/LDP/abs/html/bashver3.html # REGEXMATCHREF

NOTE: The quoting in the matching operator within the double brackets, [[ ]], is no longer necessary as of Bash version 3.2

注意:在Bash 3.2版本中,不再需要在双括号[]中引用匹配操作符中的引号

#3


20  

A good way to test if a string is a correct date is to use the command date:

测试字符串是否是正确日期的一个好方法是使用命令日期:

if date -d "${DATE}" >/dev/null 2>&1
then
  # do what you need to do with your date
else
  echo "${DATE} incorrect date" >&2
  exit 1
fi

#1


167  

You can say:

你可以说:

[[ $date =~ ^[0-9]{8}$ ]] && echo "yes"

Or more accurate:

或者更准确:

[[ $date =~ ^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])$ ]] && echo "yes"
#           |^^^^^^^^ ^^^^^^ ^^^^^^  ^^^^^^ ^^^^^^^^^^ ^^^^^^ |
#           |   |     ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^ |
#           |   |          |                   |              |
#           |   |           \                  |              |
#           | --year--   --month--           --day--          |
#           |          either 01...09      either 01..09     end of line
# start of line            or 10,11,12         or 10..29
#                                              or 30, 31

That is, you can define a regex in bash matching the format you want. This way you can do:

也就是说,您可以在bash中定义一个regex,以匹配所需的格式。这样你就可以做到:

[[ $date =~ ^regex$ ]] && echo "matched" || echo "did not match"

Note this is based on the solution by Aleks-Daniel Jakimenko in User input date format verification in bash.

注意,这是基于Aleks-Daniel Jakimenko在bash中的用户输入日期格式验证的解决方案。


In shells such as sh or fish – less equipped then bash – you can use grep:

在像sh或fish这样的贝壳中——没有装备那么bash——你可以使用grep:

(echo "$date" | grep -Eq  ^regex$) && echo "matched" || echo "did not match"

#2


28  

In bash version 3 you can use the '=~' operator:

在bash版本3中,您可以使用'=~'操作符:

if [[ "$date" =~ "[0-9]\{8\}" ]]; then
    echo "Valid date"
else
    echo "Invalid date"
fi

Reference: http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF

参考:http://tldp.org/LDP/abs/html/bashver3.html # REGEXMATCHREF

NOTE: The quoting in the matching operator within the double brackets, [[ ]], is no longer necessary as of Bash version 3.2

注意:在Bash 3.2版本中,不再需要在双括号[]中引用匹配操作符中的引号

#3


20  

A good way to test if a string is a correct date is to use the command date:

测试字符串是否是正确日期的一个好方法是使用命令日期:

if date -d "${DATE}" >/dev/null 2>&1
then
  # do what you need to do with your date
else
  echo "${DATE} incorrect date" >&2
  exit 1
fi