分页: 1 / 1

请教 bash 里怎么才能用原始的正则表达式?[已解决]

发表于 : 2015-11-07 15:52
alober
#!/usr/bin/env bash
find . -regex ".*\(\.\(ak\|au\|ax\)\|~\)"

如上,开始还可以,后来文件类型多了,这个就太长了,也不容易读。
后面这个正则表达式,怎么能让它像 perl 或 javascript 里那样简单,不要这么多转义?
我希望表达式部分能像下面这样用,请教要怎么做才能达到?

#!/usr/bin/env bash
find . -regex /.*\.(ak|au|ax)|~/

Re: 请教 bash 里怎么才能用原始的正则表达式?

发表于 : 2015-11-07 17:29
susbarbatus
用单引号

Re: 请教 bash 里怎么才能用原始的正则表达式?

发表于 : 2015-11-07 19:05
alober
susbarbatus 写了:用单引号
下面是在我的电脑上运行的结果,使用了单引号,但没有查到。是我哪里写错了?能请给出示范代码吗?

alober@pc:~/a$ ls
run.sh start.sh
alober@pc:~/a$ find . -regex '.*\.(sh|txt)|~'
alober@pc:~/a$

Re: 请教 bash 里怎么才能用原始的正则表达式?

发表于 : 2015-11-07 21:36
duguyipiao
找的是这个?

代码: 全选

-regextype type
Changes the regular expression syntax understood by  -regex  and -iregex tests which occur later on the command line.  Currently-implemented types are emacs (this is the default),  posix-awk, posix-basic, posix-egrep and posix-extended.

Re: 请教 bash 里怎么才能用原始的正则表达式?

发表于 : 2015-11-07 21:56
susbarbatus
正则有很多实现,find 的正则默认是 emacs 的,语法不太一样;比较常见的比如 egrep 风格的,可以这样指定:

代码: 全选

find . -regextype "posix-egrep" -regex '.*\.(sh|txt)|~'
P.S. 我自己试了一下,发现用这里单引号和双引号没什么区别,shell 的 Expansion 在这个位置似乎不生效?

P.P.S. 我没看懂最后的 |~ 具体是要做什么?

Re: 请教 bash 里怎么才能用原始的正则表达式?

发表于 : 2015-11-07 22:40
alober
duguyipiao 写了:找的是这个?

代码: 全选

-regextype type
Changes the regular expression syntax understood by  -regex  and -iregex tests which occur later on the command line.  Currently-implemented types are emacs (this is the default),  posix-awk, posix-basic, posix-egrep and posix-extended.
谢谢,就是这个。