简体   繁体   中英

regular expression to match files

I used the following RE:

^file(\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))? 

To match the following

 file1.bla

or

 file1.1

or

 file1.1.bla

but after:

ls "^file(\d{1,3})(\w*)(?:\.(\d{0,3})(\w*))?"

I not see any match of the relevant files why? Yael

Use ls -1 | grep "^file(\\d{1,3})(\\w*)(?:\\.(\\d{0,3})(\\w*))?" ls -1 | grep "^file(\\d{1,3})(\\w*)(?:\\.(\\d{0,3})(\\w*))?" instead. This pipes the output of ls to grep, and grep filters the input (which is piped from ls) with the regexp.

ls only supports simple matching with ? and * and []

if you need to use regex, use find

find . -regex '.*/regularexpression'

您可以使用此处和其他地方描述的外壳文件名遍历(RE的功能不那么强大)

ls -1 | perl -ne 'print if /^file(\\d{1,3})(\\w*)(?:.(\\d{0,3})(\\w*))?/'

(grep -P is supposed to use Perl regexp but on my system it doesn't seem to work. The only way to be sure you're using Perl regular expressions is to use Perl.)

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-2024 STACKOOM.COM