Chinaunix

标题: 有人写 in process regex 成功过吗? 不会是 Cygwin 版本的 Bash 有什么不一样吧? [打印本页]

作者: dove-young    时间: 2010-05-05 21:34
标题: 有人写 in process regex 成功过吗? 不会是 Cygwin 版本的 Bash 有什么不一样吧?
本帖最后由 dove-young 于 2010-05-05 21:37 编辑

我承认我看过《十二篇》,也看过《十三问》了。但是我的确没有看到关于 In process regex 的内容。也许是我找的还不够仔细。

不过这个东西看起来好像是挺郁闷的。 bash 的 manual page 和 维基百科 里面都说 bash 3.0 以后可以写 in process 的 regex。我在我的 bash
3.2.49 里面却怎么也写不出可用的 regex 的样例。

  1. $ bash --version
  2. GNU bash, version 3.2.49(23)-release (i686-pc-cygwin)
  3. Copyright (C) 2007 Free Software Foundation, Inc.
复制代码

  1. $ if [[ 192.168.0.4 =~ '[0-9].*' ]] ; then echo 222; else echo 333; fi
  2. 333
  3. if [[ 192 =~ '[0-9].*' ]] ; then echo 222; else echo 333; fi
  4. 333
  5. if [[ 192 =~ '.*' ]] ; then echo 222; else echo 333; fi
  6. 333
  7. if [[ 192 =~ '/.*/' ]] ; then echo 222; else echo 333; fi
  8. 333
  9. if [[ 192 =~ /[0-9].*/ ]] ; then echo 222; else echo 333; fi
  10. 333
复制代码
貌似只有一种情况才可以匹配

  1. if [[ 192 =~ 192 ]] ; then echo 222; else echo 333; fi
  2. 222
复制代码
但是这就不再是 regex 了吧。。。。。。。 下面是 bash man page 里面截出来的内容


  1. [[ expression ]]

  2.               An additional binary operator, =~, is available, with the same precedence as == and !=.  When it is used, the  string  to  the
  3.               right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)).  The return value
  4.               is 0 if the string matches the pattern, and 1 otherwise.  If the regular expression is  syntactically  incorrect,  the  condi-
  5.               tional  expression's  return value is 2.  If the shell option nocasematch is enabled, the match is performed without regard to
  6.               the case of alphabetic characters.  Substrings matched by parenthesized subexpressions within the regular expression are saved
  7.               in the array variable BASH_REMATCH.  The element of BASH_REMATCH with index 0 is the portion of the string matching the entire
  8.               regular expression.  The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized sub-
  9.               expression.
复制代码
下面是 维基百科 里面截出来的内容

  1. 进程内的正则表达式

  2. bash 3.0 支持进程内的正则表达式,使用下面的语法 :

  3. [[ string =~ regex ]]

  4. 正则表达式语法同regex(7) man page 所描述的一致。正则表达式匹配字符串时上述命令的退出状态为0,不匹配为1。正则表达式中用圆括号括起的子表达式可以访问shell变量 BASH_REMATCH ,如下:

  5. if [[ abcfoobarbletch =~ 'foo(bar)bl(.*)' ]]
  6. then
  7.          echo The regex matches!
  8.          echo $BASH_REMATCH      -- outputs: foobarbletch
  9.          echo ${BASH_REMATCH[1]} -- outputs: bar
  10.          echo ${BASH_REMATCH[2]} -- outputs: etch
  11. fi

  12. 使用这个语法的性能要比生成一个新的进程来运行 grep 命令优越,因为正则表达式匹配在bash进程内完成。如果正则表达式或者字符串包括空格或者shell 关键字,(诸如 '*' 或者 '?'),就需要用引号包裹。
复制代码
神啊!有人写 in process regex 成功过吗? 不会是 Cygwin 版本的 Bash 有什么不一样吧?
作者: starwing83    时间: 2010-05-06 07:01
Linux下的bash4.1.5可以,我的minSYS的3.1.17不行……
作者: waker    时间: 2010-05-06 08:08
''换""
作者: zhangshebao    时间: 2010-05-06 13:56
在cybwin bash3.2试了一下,
[[ s1 =~ s2 ]]
s1包含s2时为真,不能用规则表达式。
作者: Shell_HAT    时间: 2010-05-06 22:09
这两个系统行:
#if [[ abcfoobarbletch =~ 'foo(bar)bl(.*)' ]]; then echo matches; fi
matches
#bash --version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.
#uname -a
SunOS t1000 5.10 Generic_118833-33 sun4v sparc SUNW,Sun-Fire-T1000 Solaris

#if [[ abcfoobarbletch =~ 'foo(bar)bl(.*)' ]]; then echo matches; fi
matches
#bash --version
GNU bash, version 3.00.16(1)-release (powerpc-ibm-aix5.1)
Copyright (C) 2004 Free Software Foundation, Inc.
#uname -a
AIX aix 3 5 00C97AC04C00 powerpc unknown AIX

这两个系统不行:
#if [[ abcfoobarbletch =~ 'foo(bar)bl(.*)' ]]; then echo matches; fi
#bash --version
GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
#uname -a
Linux ubuntu 2.6.24-22-generic #1 SMP Mon Nov 24 19:35:06 UTC 2008 x86_64 GNU/Linux

#if [[ abcfoobarbletch =~ 'foo(bar)bl(.*)' ]]; then echo matches; fi
#bash --version
GNU bash, version 4.0.24(0)-release (amd64-portbld-freebsd7.2)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
#uname -a
FreeBSD freebsd.unix-center.net 7.2-RELEASE-p4 FreeBSD 7.2-RELEASE-p4 #0: Fri Oct  2 08:22:32 UTC 2009     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64

作者: dove-young    时间: 2010-05-07 13:16
看来还是S了这条心了吧。。。。 有这种regex的东西还是pipe到sed/awk吧。。。。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2