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

sh/bash/dash/ksh/zsh等Shell脚本
回复
alober
帖子: 135
注册时间: 2010-07-13 17:04

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

#1

帖子 alober » 2015-11-07 15:52

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

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

#!/usr/bin/env bash
find . -regex /.*\.(ak|au|ax)|~/
上次由 alober 在 2015-11-07 22:41,总共编辑 1 次。
头像
susbarbatus
帖子: 2966
注册时间: 2010-04-10 16:14
系统: Arch Linux

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

#2

帖子 susbarbatus » 2015-11-07 17:29

用单引号
沉迷将棋中……
alober
帖子: 135
注册时间: 2010-07-13 17:04

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

#3

帖子 alober » 2015-11-07 19:05

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 里怎么才能用原始的正则表达式?

#4

帖子 duguyipiao » 2015-11-07 21:36

找的是这个?

代码: 全选

-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 里怎么才能用原始的正则表达式?

#5

帖子 susbarbatus » 2015-11-07 21:56

正则有很多实现,find 的正则默认是 emacs 的,语法不太一样;比较常见的比如 egrep 风格的,可以这样指定:

代码: 全选

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

P.P.S. 我没看懂最后的 |~ 具体是要做什么?
沉迷将棋中……
alober
帖子: 135
注册时间: 2010-07-13 17:04

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

#6

帖子 alober » 2015-11-07 22:40

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.
谢谢,就是这个。
回复