r-developer
r-developer

Reputation: 527

regular expression for perl : windows programming

I want a perl regular expression which will print the files name those have only .txt extension at the end nothing else.

I have written a reg exp but it is printing all the files with extension .txt also .txt* files. That I dont want

if($filename=~m/xxx\\.txt/i)

I have now also tried the following, but it is also not working

if($filename=~m/xxx\\.txt$/i)

Upvotes: 0

Views: 105

Answers (2)

Hajjat
Hajjat

Reputation: 300

Would it be that Windows is adding funny things at end of file names? Try:

if($filename =~ /\.txt\s*/)

Upvotes: 0

Miller
Miller

Reputation: 35198

you need the $ boundary to indicate end of string.

if ($filename =~ m/\.txt$/i)

Note, you can also use a glob to get all the text files

my @textfiles = <*.txt>;

Upvotes: 2

Related Questions