使用Bash正则表达式匹配(=〜),其中正则表达式包含引号(“字符") [英] Using Bash regex match (=~) where regex includes quotes (" characters)

查看:74
本文介绍了使用Bash正则表达式匹配(=〜),其中正则表达式包含引号(“字符")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Bash正则表达式匹配(使用=〜运算符)来匹配包含引号的字符串.例如,我有以下字符串,并且我想提取引号之间的文本:

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:

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" =~ '".*"' ]]

之所以失败,是因为Bash将其解释为字符串匹配而不是正则表达式.

That fails because Bash interprets this as a string match rather than a regex.

然后,我尝试使用\来对引号进行转义,例如:

Then I tried to escape the quotes with \ like so:

[[ "$foo" =~ \".*\" ]]

那会失败(实际上,不会.如果\和]之间没有空格,它会失败,但是这里的版本就可以了.),因为第一个\是纯bash文本,并且无法转义引号(我认为.VIM中的颜色表示第二个引号已转义,但第一个引号未转义,并且运行脚本失败).

That fails ( 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).

那么我有什么方法可以转义字符而无需将正则表达式匹配转换为字符串匹配?

So is there some way I can escape the " characters without transforming the regex match into a string match?

推荐答案

实际上,您的第二次尝试在bash 3和4中对我有用:

Actually, your second attempt works for me in bash 3 and 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!"

但是,让我们稍稍讨论一下理论,这完全与bash如何解释整个表达式有关.只要不对正则表达式字符本身加引号,表达式的其余部分都可以加引号而没有副作用:

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:

$ [[ $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!"

这篇关于使用Bash正则表达式匹配(=〜),其中正则表达式包含引号(“字符")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆