简单的 Bash 和 RegEx 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7438566/
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
Simple Bash and RegEx Questions
提问by Gray Race
I'm unable to get a pattern to match properly using regex in bash 4.1. I've read some information on about differences in quotes vs not quotes but I don't think that is my problem.
我无法在 bash 4.1 中使用正则表达式正确匹配模式。我已经阅读了一些有关引号与非引号差异的信息,但我认为这不是我的问题。
My goal is to check and make sure that the script is provided a valid ID. A valid ID in this case is a string of 9 digits. It's my understand that a regex expression for that is \d{9}. Given that here is my code snippet to check:
我的目标是检查并确保为脚本提供了有效的 ID。在这种情况下,有效 ID 是一串 9 位数字。据我所知,它的正则表达式是 \d{9}。鉴于这是我要检查的代码片段:
id=
if [[ $id =~ \d{9} ]]; then
echo "This is a vaild ID"
else
echo "This is not a vaild ID"
fi
Then call the script with:
然后调用脚本:
./script 987654321
Something obvious that I am missing?
一些明显的东西,我失踪了?
回答by stema
As others already answered you should use [0-9]{9}because \dis not supported. But another important part is, you have to use anchors!
正如其他人已经回答的那样,您应该使用[0-9]{9}因为\d不支持。但另一个重要的部分是,你必须使用锚点!
^[0-9]{9}$
The ^anchors the regex to the start of the string and the $anchors it to the end. If you don't use them your regex will match on every string that contains 9 digits in a sequence, like "abc123456789", "asdf123456789zui" or "123456789FOOBAR".
将^正则表达式锚定到字符串的开头,并将其$锚定到结尾。如果您不使用它们,您的正则表达式将匹配序列中包含 9 位数字的每个字符串,例如“abc123456789”、“asdf123456789zui”或“123456789FOOBAR”。
回答by David W.
This will work: if [[ $id =~ [[:digit:]]{9} ]] – David W. 11 hours ago
这将起作用:if [[ $id =~ [[:digit:]]{9} ]] – David W. 11 小时前
@David I tried that on bash and it didn't seem to work. – Vivin Paliath 10 hours ago
@David 我在 bash 上尝试过,但似乎没有用。– Vivin Paliath 10 小时前
I've just written a test program:
我刚刚写了一个测试程序:
#! /bin/bash
for id in 123456789 12345689 1234567890 987654321
do
if [[ $id =~ ^[[:digit:]]{9}$ ]]
then
echo "$id is 9 digits long"
else
echo "$id is bad"
fi
done
And I got the following output:
我得到了以下输出:
DaveBook:~ david$ ./test.sh
123456789 is 9 digits long
12345689 is bad
1234567890 is bad
987654321 is 9 digits long
DaveBook:~ david$
I'm using BASH 3.2.48 for Mac OS X and Bash 4.1.10(4) for Cygwin (Wow, the Mac version is thatold?). I haven't tested this on a Linux box.
我在 Mac OS X 上使用 BASH 3.2.48,在 Cygwin 上使用 Bash 4.1.10(4)(哇,Mac 版本这么旧?)。我还没有在 Linux 机器上测试过这个。
What version are you using? Are you doubling the square braces around :digit:? It has to be [[:digit:]]and not [:digit:].
你用的是什么版本?你把周围的方括号加倍了:digit:吗?它必须是[[:digit:]]和不是[:digit:]。
I also realized I need the ^and $anchors in there because you don't want to match foo123456789baror 1234567890123456790. Was this an issue you had?
我也意识到我需要^和$锚点在那里,因为你不想匹配foo123456789baror 1234567890123456790。这是您遇到的问题吗?
The thing is that [[:digit:]]shouldhave worked. I've used it in many Bash shell scripts thinking it was fairly universal. However, if you have a BASH shell where it doesn't work, I'm going to have to stop using it.
事情是[[:digit:]]应该奏效的。我在许多 Bash shell 脚本中使用过它,认为它相当普遍。但是,如果您有一个 BASH shell 不起作用,我将不得不停止使用它。
回答by Vivin Paliath
Try this:
尝试这个:
if [[ $id =~ [0-9]{9} ]]; then
It looks like bash doesn't recognize \das [0-9].
看起来 bash 无法识别\d为[0-9].
Bash usesthe Extended Regular Expression dialect, which doesn't support \d.
Bash使用的扩展正则表达式的方言,它不支持\d。
According to ERE's grammar (lexical conventions), escaped characters are of the form \SPEC_CHAR. SPEC_CHARcan be any one of the following:
根据 ERE 的语法(词汇约定),转义字符的形式为\SPEC_CHAR. SPEC_CHAR可以是以下任何一种:
^ . [ $ ( ) |
* + ? { \
回答by Asmor
The problem is \d gets turned into d before it's interpreted. Try double escaping.
问题是 \d 在解释之前就变成了 d 。尝试双重转义。
\d{9} => d{9}
\d{9} => \d{9}
Alternatively, you could put it in quotes
或者,你可以把它放在引号中
if [[ $id =~ "\d{9}" ]]

