Im trying to get a regexp (in bash) to identify files with only the following extensions : tgz, tar.gz, TGZ and TAR.GZ. I tried several ones but cant get it to work. Im using this regexp to select only files files with those extensions to do some work with them :
我试图获得一个正则表达式(在bash中)来识别只有以下扩展名的文件:tgz,tar.gz,TGZ和TAR.GZ.我尝试了几个,但不能让它工作。我正在使用此正则表达式仅选择具有这些扩展名的文件文件,以便对它们进行一些操作:
if [ -f $myregexp ]; then
.....
fi
thanks.
6 个解决方案
#1
4
Try this:
#!/bin/bash
# no case match
shopt -s nocasematch
matchRegex='.*\.(tgz$)|(tar\.gz$)'
for f in *
do
# display filtered files
[[ -f "$f" ]] && [[ "$f" =~ "$matchRegex" ]] && echo "$f";
done
#2
1
I have found an elegant way of doing this:
我找到了一种优雅的方式:
shopt -s nocasematch
for file in *;
do
[[ "$file" =~ .*\.(tar.gz|tgz)$ ]] && echo $file
done
This may be good for you since you seems to want to use the if and a bash regex. The =~
operator allow to check if the pattern is matching a given expression. Also shopt -s nocasematch
has to be set to perfom a case insensitive match.
这可能对你有好处,因为你似乎想要使用if和bash正则表达式。 =〜运算符允许检查模式是否与给定表达式匹配。另外shopt -s nocasematch必须设置为不区分大小写的匹配。
#3
0
Use this pattern
使用此模式
.*\.{1}(tgz|tar\.gz)
But how to make a regular expression case-insensitive? It depends on the language you use. In JavaScript they use /pattern/i
, in which, i
denotes that the search should be case-insensitive. In C# they use RegexOptions enumeration.
但是如何使正则表达式不区分大小写呢?这取决于您使用的语言。在JavaScript中,他们使用/ pattern / i,其中,i表示搜索应该不区分大小写。在C#中,他们使用RegexOptions枚举。
#4
0
Depends on where you want to use this regex. If with GREP, then use egrep
with -i parameter, which stands for "ignore case"
取决于您想要使用此正则表达式的位置。如果使用GREP,那么使用带有-i参数的egrep,代表“忽略大小写”
egrep -i "(\.tgz)|(\.tar\.gz)$"
#5
0
Write 4 regexes, and check whether the file name matches any of them. Or write 2 case-insensitive regexes.
编写4个正则表达式,并检查文件名是否与其中任何一个匹配。或者编写2个不区分大小写的正则表达式。
This way the code will be much more readable (and easier) than writing 1 regex.
这样,代码将比编写1个正则表达式更具可读性(也更容易)。
#6
0
You can even do it without a regex (a bit wordy though):
你甚至可以在没有正则表达式的情况下做到这一点(虽然有点罗嗦):
for f in *.[Tt][Gg][Zz] *.[Tt][Aa][Rr].[Gg][Zz]; do
echo $f
done
#1
4
Try this:
#!/bin/bash
# no case match
shopt -s nocasematch
matchRegex='.*\.(tgz$)|(tar\.gz$)'
for f in *
do
# display filtered files
[[ -f "$f" ]] && [[ "$f" =~ "$matchRegex" ]] && echo "$f";
done
#2
1
I have found an elegant way of doing this:
我找到了一种优雅的方式:
shopt -s nocasematch
for file in *;
do
[[ "$file" =~ .*\.(tar.gz|tgz)$ ]] && echo $file
done
This may be good for you since you seems to want to use the if and a bash regex. The =~
operator allow to check if the pattern is matching a given expression. Also shopt -s nocasematch
has to be set to perfom a case insensitive match.
这可能对你有好处,因为你似乎想要使用if和bash正则表达式。 =〜运算符允许检查模式是否与给定表达式匹配。另外shopt -s nocasematch必须设置为不区分大小写的匹配。
#3
0
Use this pattern
使用此模式
.*\.{1}(tgz|tar\.gz)
But how to make a regular expression case-insensitive? It depends on the language you use. In JavaScript they use /pattern/i
, in which, i
denotes that the search should be case-insensitive. In C# they use RegexOptions enumeration.
但是如何使正则表达式不区分大小写呢?这取决于您使用的语言。在JavaScript中,他们使用/ pattern / i,其中,i表示搜索应该不区分大小写。在C#中,他们使用RegexOptions枚举。
#4
0
Depends on where you want to use this regex. If with GREP, then use egrep
with -i parameter, which stands for "ignore case"
取决于您想要使用此正则表达式的位置。如果使用GREP,那么使用带有-i参数的egrep,代表“忽略大小写”
egrep -i "(\.tgz)|(\.tar\.gz)$"
#5
0
Write 4 regexes, and check whether the file name matches any of them. Or write 2 case-insensitive regexes.
编写4个正则表达式,并检查文件名是否与其中任何一个匹配。或者编写2个不区分大小写的正则表达式。
This way the code will be much more readable (and easier) than writing 1 regex.
这样,代码将比编写1个正则表达式更具可读性(也更容易)。
#6
0
You can even do it without a regex (a bit wordy though):
你甚至可以在没有正则表达式的情况下做到这一点(虽然有点罗嗦):
for f in *.[Tt][Gg][Zz] *.[Tt][Aa][Rr].[Gg][Zz]; do
echo $f
done