只想学习BASH_REMATCH。
在ubuntu 16.10上,我为BASH_REMATCH写了一个小脚本:
#!/bin/bash
x='sadas123fnrejkwhfklde'
if [[ $x =~ ([0-9]*) ]]
then
echo "match"
echo ${BASH_REMATCH[1]}
else
echo "not match"
fi
然而,当/ i运行此脚本时,只有"匹配"出现,而不是捕获的比赛!
bash版本是:
$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
感谢任何反馈。
答案 0 :(得分:2)
([0-9])*
匹配字符串中任何位置的零个或多个数字。因此,它产生一个空字符串,这是一个有效的匹配。
将表达式更改为([0-9]+)
,使其与一个或多个数字匹配。
#!/bin/bash
x='sadas123fnrejkwhfklde'
if [[ $x =~ ([0-9]+) ]]
then
echo "match"
echo ${BASH_REMATCH[1]}
else
echo "not match"
fi
产量
match
123