Bash 脚本正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7159441/
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
Bash scripting regular expressions
提问by MeanwhileInHell
I'm trying to match a version number from a string in the format 4.6, or 2.8. I have the following which I will eventually use in a function in my .bashrc file to find the OS version:
我正在尝试从 4.6 或 2.8 格式的字符串中匹配版本号。我有以下内容,我最终将在我的 .bashrc 文件中的一个函数中使用它来查找操作系统版本:
function test () {
string="abc ABC12 123 3.4 def";
echo `expr match "$string" '[0-9][.][0-9]'`
}
However, this doesn't match the 3.4 in the string. Can anyone point me in the right direction here?
但是,这与字符串中的 3.4 不匹配。任何人都可以指出我正确的方向吗?
Thanks.
谢谢。
回答by OpenSauce
First, you can drop the echo- exprprints its result to stdout in any case.
首先,在任何情况下,您都可以删除echo- 将expr其结果打印到标准输出。
Second, your regex needs brackets (otherwise it prints the number of characters matched, not the match itself), and it needs to begin with .*.
其次,您的正则表达式需要括号(否则它会打印匹配的字符数,而不是匹配本身),并且它需要以.*.
expr match "$string" '.*\([0-9][.][0-9]\)'
From the info exprpage:
从info expr页面:
STRING : REGEX'
Perform pattern matching. The arguments are converted to strings and the second is considered to be a (basic, a la GNU `grep') regular expression, with a `^' implicitly prepended. The first argument is then matched against this regular expression. If the match succeeds and REGEX uses `\(' and `\)', the `:' expression returns the part of STRING that matched the subexpression; otherwise, it returns the number of characters matched.
字符串:正则表达式'
Perform pattern matching. The arguments are converted to strings and the second is considered to be a (basic, a la GNU `grep') regular expression, with a `^' implicitly prepended. The first argument is then matched against this regular expression. If the match succeeds and REGEX uses `\(' and `\)', the `:' expression returns the part of STRING that matched the subexpression; otherwise, it returns the number of characters matched.
回答by glenn Hymanman
Depending on your version of bash, there's no need to call out to expr:
根据您的 bash 版本,无需调用 expr:
$ [[ "abc ABC12 123 3.4 def" =~ [0-9][.][0-9] ]] && echo ${BASH_REMATCH[0]}
3.4
回答by Jens
Thinking outside the box: if what you are looking for is determining the OS version in a script, just use uname -ror uname -v(it's POSIX). Messing with regular expression is likely to have issues as each OS may have different ways to express its version. OS vendors are so creative in inventing version jumps forward and backward, some have letters in there, and even roman numerals are not unheard of (think System V).
跳出框框思考:如果您正在寻找的是确定脚本中的操作系统版本,只需使用uname -ror uname -v(它是 POSIX)。使用正则表达式可能会出现问题,因为每个操作系统可能有不同的方式来表达其版本。操作系统供应商在发明版本向前和向后跳跃方面非常有创意,有些在那里有字母,甚至罗马数字也不是闻所未闻(想想System V)。
See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uname.html
请参阅http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uname.html
I use in my .profile a snippet like this:
我在我的 .profile 中使用了这样的片段:
case "`uname -sr`" in
(*BSD*) OS=`uname -s`;;
(SunOS\ 4*) OS=SunOS;;
(SunOS\ 5*) OS=Solaris;;
(IRIX\ 5*) OS=IRIX;;
(HP*) OS=HP-UX;;
(Linux*) OS=Linux;;
(CYGWIN*) OS=Cygwin;;
(*) OS=generic
esac
回答by bashfu
On Mac OS X 10.6.8:
在 Mac OS X 10.6.8 上:
# cf. http://tldp.org/LDP/abs/html/refcards.html#AEN22429
string="abc ABC12 123 3.4 def"
expr "$string" : '.*\([0-9].[0-9]\)' # 3.4
回答by Jacek Kaniuk
expr match "$string" '.*[0-9][.][0-9]'

