简体   繁体   中英

regex quantifiers in bash --simple vs extended matching {n} times

I'm using the bash shell and trying to list files in a directory whose names match regex patterns. Some of these patterns work, while others don't. For example, the * wildcard is fine:

$ls FILE_*
FILE_123.txt    FILE_2345.txt   FILE_789.txt

And the range pattern captures the first two of these with the following:

$ls FILE_[1-3]*.txt
FILE_123.txt    FILE_2345.txt

but not the filename with the "7" character after "FILE_", as expected. Great. But now I want to count digits:

$ls FILE_[0-9]{3}.txt 
ls: FILE_[0-9]{3}.txt: No such file or directory

Shouldn't this give me the filenames with three numeric digits following "FILE_" (ie FILE_123.txt and FILE_789.txt , but not FILE_2345.txt ) Can someone tell me how I should be using the {n} quantifier (ie "match this pattern n times)?

ls uses with glob pattern , you can not use {3} . You have to use FILE_[0-9][0-9][0-9].txt . Or, you could the following command.

ls | grep -E "FILE_[0-9]{3}.txt"

Edit:

Or, you also use find command.

find . -regextype egrep -regex '.*/FILE_[0-9]{3}\.txt'

The .*/ prefix is needed to match a complete path. On Mac OS X :

find -E . -regex ".*/FILE_[0-9]{3}\.txt"

Bash filename expansion does not use regular expressions. It uses glob pattern matching , which is distinctly different, and what you're trying with FILE_[0-9]{3}.txt does brace expansion followed by filename expansion. Even bash 's extended globbing feature doesn't have an equivalent to regular expression's {N} , so as already mentioned you have to use FILE_[0-9][0-9][0-9].txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM