庆典的正则表达式加上引号? [英] bash regex with quotes?

查看:141
本文介绍了庆典的正则表达式加上引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code

number=1
if [[ $number =~ [0-9] ]]
then
  echo matched
fi

工作。如果我尝试在正则表达式使用引号,但是,它会停止:

works. If I try to use quotes in the regex, however, it stops:

number=1
if [[ $number =~ "[0-9]" ]]
then
  echo matched
fi

我试过\\ [0-9 \\],太。我缺少什么?

有趣的是, bash的高级脚本指南表明这应该工作。

Funnily enough, bash advanced scripting guide suggests this should work.

猛砸版本3.2.39。

Bash version 3.2.39.

推荐答案

这是改变 3.1和3.2 <之间/一>。猜猜高级指南需要​​更新。

It was changed between 3.1 and 3.2. Guess the advanced guide needs an update.

这是新的简短描述
  功能加入到庆典 - 3.2自
  庆典-3.1的释放。和往常一样,
  手册(DOC / bash.1)是地方
  寻找完整的描述。

This is a terse description of the new features added to bash-3.2 since the release of bash-3.1. As always, the manual page (doc/bash.1) is the place to look for complete descriptions.


      
  1. 在Bash的新功能

  2.   

剪断

F。引用字符串参数的
  [命令的=〜运营商现在的力量
      串匹配,与其它模式匹配运算

f. Quoting the string argument to the [[ command's =~ operator now forces string matching, as with the other pattern-matching operators.

不幸的是这将使用脚本,除非你有变量有识之士店模式打破现有的报价,而是直接使用正则表达式中的。下面的例子。

Sadly this'll break existing quote using scripts unless you had the insight to store patterns in variables and use them instead of the regexes directly. Example below.

$ bash --version
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
$ number=2
$ if [[ $number =~ "[0-9]" ]]; then echo match; fi
$ if [[ $number =~ [0-9] ]]; then echo match; fi
match
$ re="[0-9]"
$ if [[ $number =~ $re ]]; then echo MATCH; fi
MATCH

$ bash --version
GNU bash, version 3.00.0(1)-release (i586-suse-linux)
Copyright (C) 2004 Free Software Foundation, Inc.
$ number=2
$ if [[ $number =~ "[0-9]" ]]; then echo match; fi
match
$ if [[ "$number" =~ [0-9] ]]; then echo match; fi
match

这篇关于庆典的正则表达式加上引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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