Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex to match filenames

Status
Not open for further replies.

sun9

Programmer
Dec 13, 2006
31
US
I am trying to create a regexp to match in a given directory all filenames without any extenstion, but I am having trouble with it-
My regular exp prints the files with the . extension under my directory
Code:
grep {/.*\..*$/ and $_ ne '.' and $_ ne '..' }
But if I try something like
Code:
grep {$_ ne '/.*\..*$/' and $_ ne '.' and $_ ne '..' }
I get all the file names even those with the .extensions. Pls let me know what I am doing wrong here.
Thanks.
 
maybe:

Code:
grep {!/\..+$/ and $_ ne '.' and $_ ne '..' }

that will match files and folders though, not just files.

- Kevin, perl coder unexceptional!
 
It seems to me that you might be overcomplicating this. How about:

Code:
grep {! /\./}

All files without a '.'. Obviously will also filter the special files '.' and '..' as well.
 
I thought of that too but some files are named:

this.foo.bar

so "this.foo" might not be considered a file extension but a filename.

- Kevin, perl coder unexceptional!
 
Kevin, I understand where your logic is coming from. However, if a file is named "this.foo", then by definition .foo is the new extension even if there was a previous .bar suffix that was removed.

sun9, your problem arises with this part of your grep:

Code:
grep {[COLOR=red]$_ ne '/.*\..*$/'[/color] and $_ ne '.' and $_ ne '..' }

You don't really want a string comparison there, instead you want a negated regex test such as:

Code:
grep {[COLOR=green]$_ !~ /.*\..*$/[/color] and $_ ne '.' and $_ ne '..' }

However, as I said in my example, your regex can be simplied. Surrounded a dot by "anything" is redundant. Just remove the anything. Then simply recognize that this regex will also capture the special files '.' and '..', and you are left with:

Code:
grep {$_ !~ /\./}

Which can be perlishly simplified to just:

Code:
grep {! /\./}
 
yes, if the suffix was removed then .foo would become the new file extension, but if it's not removed this.foo is the filename and .bar is the extension/suffix. A dot in a filename is legal, as far as I know. So to check only for a dot might be too general?

- Kevin, perl coder unexceptional!
 
Yes, of course a dot in a filename is legal and common. But he asked for:

sun9 said:
... all filenames without any extenstion ...

While it is possible to have filenames with dots in the name in addition to the extension, it's not possible to have filenames with dots and not have an extension. The only possible exception would be the boundary conditions: "foo." and ".foo", which are legal file names.

However, I was not trying to help him decide what definition he wanted to follow for these types of special cases. Instead, I simply wanted to let him know that in addition to fixing his regex, it could be simplified a lot.

shrug
 
makes perfect sense.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top