yael
yael

Reputation: 2913

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

Upvotes: 0

Views: 175

Answers (4)

Imre L
Imre L

Reputation: 6249

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

if you need to use regex, use find

find . -regex '.*/regularexpression'

Upvotes: 2

c-urchin
c-urchin

Reputation: 4504

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.)

Upvotes: 0

Ming-Tang
Ming-Tang

Reputation: 17651

Use 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.

Upvotes: 2

bjg
bjg

Reputation: 7477

What you could use is shell filename globbing (less powerful matching that REs) described here and elsewhere

Upvotes: 0

Related Questions