0

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)

2 Answers 2

2

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>;
Sign up to request clarification or add additional context in comments.

1 Comment

Use exactly what I've given you. In your example you have an escaped backslach, along with the literal string xxx. You just need \.txt$ include the regex.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.