[tpm] Another Regex question

Liam R E Quin liam at holoweb.net
Tue Oct 23 09:55:16 PDT 2012


On Tue, 2012-10-23 at 10:57 -0400, Chris Jones wrote:

> I only want to keep files with the following pattern:
> SomeFIle-Name.inp
> SomeFIle-Name.pd2
> 
> Any other files should be deleted.  Unfortunately my simple search 
> did not separate files such as:
> SomeFIle-Name.r2.inp
> SomeFIle-Name.r2.pd2
> SimFile-Name.r2 - Baseline Design.inp

So, you want t okeep files whose names are of the form
    ^           # start of the name
    [a-z]+      # letters
    -[a-z]+     # dash followed by more letters
    \.(inp|pd2) # file ends in .inp or .pd2
    $           # end of string

and allow upper and lower case letters, so use the i (case insnsitive)
flag.

sub iswanted($)
{
   my ($name) = (@_);

   return $name =~ m{
      ^           # start of the name
      [a-z]+      # letters
      -[a-z]+     # dash followed by more letters
      \.(inp|pd2) # file ends in .inp or .pd2
      $           # end of string
   }ix;
}

Liam

-- 
Liam Quin - the barefoot typographer - http://www.holoweb.net/~liam/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml
Co-author, 5th edition of "Beginning XML", Wrox, Summer 2012



More information about the toronto-pm mailing list