使用 Bash 正则表达式匹配 (=~),其中正则表达式包含引号(" 字符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24170397/
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
Using Bash regex match (=~) where regex includes quotes (" characters)
提问by DroidFreak
I want to use Bash regex matching (with the =~ operator) to match a string which includes quotes. Say for example I have the following string and I want to extract the text between quotes:
我想使用 Bash 正则表达式匹配(使用 =~ 运算符)来匹配包含引号的字符串。举例来说,我有以下字符串,我想提取引号之间的文本:
foo='"Hello World!"'
My first try was to put the regex in strong quotes like so to force the quotes to be regular characters.
我的第一次尝试是将正则表达式放在强引号中,以强制引号为常规字符。
[[ "$foo" =~ '".*"' ]]
That fails because Bash interprets this as a string match rather than a regex.
失败是因为 Bash 将其解释为字符串匹配而不是正则表达式。
Then I tried to escape the quotes with \ like so:
然后我尝试用 \ 转义引号,如下所示:
[[ "$foo" =~ \".*\" ]]
That fails (EDIT: Actually, it doesn't. It fails if there's no space between \" and ]] but the version here works just fine.) because the first \ is in plain bash text and fails to escape the quote (I think. The coloring in VIM indicates that the second quote is escaped but not the first and running the script fails).
那失败了(编辑:实际上,它没有。如果 \" 和 ]] 之间没有空格,它会失败,但这里的版本工作得很好。)因为第一个 \ 是纯 bash 文本并且无法转义引号(我想想。VIM 中的颜色表明第二个引号被转义,但不是第一个并且运行脚本失败)。
So is there some way I can escape the " characters without transforming the regex match into a string match?
那么有什么方法可以在不将正则表达式匹配转换为字符串匹配的情况下转义 " 字符?
回答by kojiro
Actually, your second attempt works for me in bash 3 and 4:
实际上,您的第二次尝试在 bash 3 和 4 中对我有用:
$ echo "$BASH_VERSION"
3.2.51(1)-release
$ echo "$foo"
"Hello World!"
$ [[ "$foo" =~ \".*\" ]] && echo $BASH_REMATCH
"Hello World!"
$ echo "$BASH_VERSION"
4.3.18(1)-release
$ echo "$foo"
"Hello World!"
$ [[ "$foo" =~ \".*\" ]] && echo "${BASH_REMATCH[0]}"
"Hello World!"
However, to talk theory for a second, it all has to do with how bash interprets the expression as a whole. As long as the regular-expression characters themselves aren't quoted, the rest of the expression can be quoted without side-effects:
然而,先谈谈理论,这一切都与 bash 如何将表达式作为一个整体进行解释有关。只要正则表达式字符本身没有被引用,表达式的其余部分就可以被引用而不会产生副作用:
$ [[ $foo =~ '"'.*'"' ]] && echo $BASH_REMATCH
"Hello World!"
but perhaps the easiest way of all is to use a second variable to hold the regex itself.
但也许最简单的方法是使用第二个变量来保存正则表达式本身。
$ exp='".*"'
$ [[ $foo =~ $exp ]] && echo $BASH_REMATCH
"Hello World!"