#!/usr/bin/env bash
find . -regex ".*\(\.\(ak\|au\|ax\)\|~\)"
如上,开始还可以,后来文件类型多了,这个就太长了,也不容易读。
后面这个正则表达式,怎么能让它像 perl 或 javascript 里那样简单,不要这么多转义?
我希望表达式部分能像下面这样用,请教要怎么做才能达到?
#!/usr/bin/env bash
find . -regex /.*\.(ak|au|ax)|~/
请教 bash 里怎么才能用原始的正则表达式?[已解决]
-
- 帖子: 135
- 注册时间: 2010-07-13 17:04
请教 bash 里怎么才能用原始的正则表达式?[已解决]
上次由 alober 在 2015-11-07 22:41,总共编辑 1 次。
- susbarbatus
- 帖子: 2966
- 注册时间: 2010-04-10 16:14
- 系统: Arch Linux
-
- 帖子: 135
- 注册时间: 2010-07-13 17:04
Re: 请教 bash 里怎么才能用原始的正则表达式?
下面是在我的电脑上运行的结果,使用了单引号,但没有查到。是我哪里写错了?能请给出示范代码吗?susbarbatus 写了:用单引号
alober@pc:~/a$ ls
run.sh start.sh
alober@pc:~/a$ find . -regex '.*\.(sh|txt)|~'
alober@pc:~/a$
- duguyipiao
- 帖子: 158
- 注册时间: 2011-09-13 10:31
Re: 请教 bash 里怎么才能用原始的正则表达式?
找的是这个?
代码: 全选
-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.
- susbarbatus
- 帖子: 2966
- 注册时间: 2010-04-10 16:14
- 系统: Arch Linux
Re: 请教 bash 里怎么才能用原始的正则表达式?
正则有很多实现,find 的正则默认是 emacs 的,语法不太一样;比较常见的比如 egrep 风格的,可以这样指定:
P.S. 我自己试了一下,发现用这里单引号和双引号没什么区别,shell 的 Expansion 在这个位置似乎不生效?
P.P.S. 我没看懂最后的 |~ 具体是要做什么?
代码: 全选
find . -regextype "posix-egrep" -regex '.*\.(sh|txt)|~'
P.P.S. 我没看懂最后的 |~ 具体是要做什么?
沉迷将棋中……
-
- 帖子: 135
- 注册时间: 2010-07-13 17:04
Re: 请教 bash 里怎么才能用原始的正则表达式?
谢谢,就是这个。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.