RE: Regular expression quiz


Subject: RE: Regular expression quiz
From: Leif Sawyer (lsawyer@gci.com)
Date: Fri May 03 2002 - 16:18:13 AKDT


Mike Barsalou ponders...
> I have files in a directory like this:
>
> mike-1.2.3
> mike-jan-1.2.3
> mike-feb-3.4.5
> mike-mar-1.2.3
> mike-dec-4.5.6
> dave
> dave-feb-1.2.3
>
>
> AND
>
> I have a file that contains a list of file names like this:
>
> mike-1.2.3
> mike-jan-1.2.3
> mike-dec-4.5.6
> dave
> dave-feb-1.2.3
>
>
> I want to move only the files listed in the this file to
> another directory.
>
> Here is the quiz:
>
> What regular expression do I use?
>

for i in `cat myfile | grep -E '^[a-z]{4}(-[a-z]{3}-[1-9\.]{5})?$' `;
do
  mv $i newdir
done

##
breaking the regexp down:

1 - ^
2 - [a-z]{4}
3 - (-[a-z]{3}-[1-9\.]{5})?
3a - -[a-z]{3}-[1-9\.]{5}
4 - $

line 1 : fix the start of the regexp to the beginning of the line
line 2 : you've specified files have a 4-letter lowercase prefix, so this
          limits the first part of the name to LC alpha [a-z] and a limit of
{4}

line 3 : There can be an optional -mon-1.2.3 tag at the end of the filename,
         so the ( )? tags allow for 0 or 1 matches of the enclosed regexp
         -[a-z]{3}-[1-9\.]{5}

line 3a : the optional regexp to limit to the hyphen, followed by a 3-letter
month [a-z]{3}
          then the #.#.# suffix.

line 4 : tie the end of the regexp to the end of line.

Note that in line 3a, the [1-9\.]{5} is an ugly hack. it can be confused
by things which don't match what we're looking for (like 1..3., or 12345)

So, since this is really an incomplete answer, the Ximian shirt should go
to the person who correctly fixes line 3a..

:-)

Leif

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Fri May 03 2002 - 16:18:16 AKDT