Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Find Files Without An Extension

Last post 04-16-2008, 7:33 PM by Aussie Susan. 7 replies.
Sort Posts: Previous Next
  •  12-08-2006, 10:04 AM 25352

    Find Files Without An Extension

    I want to identify files that do NOT have an extension so they can be parsed for PHP.  This is for a web site .htaccess file running on a Linux/Apache web server.

    I have tried to filter for files not containing a dot (.)

    Code I have tried is:

    <FilesMatch "[^.]*">
    ForceType application/x-httpd-php
    </FilesMatch>

     

    From Apache.org about the <filesMatch> directive:

    The <FilesMatch> directive provides for access control by filename, just as the <Files> directive does. However, it accepts a regular expression. For example:

       <FilesMatch "\.(gif|jpe?g|png)$">

    would match most common Internet graphics formats.

     

    Filed under: ,
  •  12-08-2006, 10:12 AM 25353 in reply to 25352

    Re: Find Files Without An Extension

    Remember that a '.' in a RegEx means match any character so you need to escape it:

     [^\.]

  •  12-08-2006, 10:21 AM 25355 in reply to 25353

    Re: Find Files Without An Extension

    jeromewilson:

    Remember that a '.' in a RegEx means match any character so you need to escape it:

     [^\.]

    That is incorrect.  Within a character class, which is what was being used, a period only matches a period.  Escaping it, while legal, is redundant. 

    Michael 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  12-08-2006, 10:37 AM 25357 in reply to 25352

    Re: Find Files Without An Extension

    daveberk:

    I want to identify files that do NOT have an extension so they can be parsed for PHP.  This is for a web site .htaccess file running on a Linux/Apache web server.

    I have tried to filter for files not containing a dot (.)

    Code I have tried is:

    <FilesMatch "[^.]*">
    ForceType application/x-httpd-php
    </FilesMatch>

     

    From Apache.org about the <filesMatch> directive:

    The <FilesMatch> directive provides for access control by filename, just as the <Files> directive does. However, it accepts a regular expression. For example:

       <FilesMatch "\.(gif|jpe?g|png)$">

    would match most common Internet graphics formats.

     

    If you look closely at watch you have and the website example you might see what the problem is, boundries

    What you have is unbounded and will match anything not a period.  That doesn't mean the input won't contain a period just  that the period wont be included in the match or matches.  The regex would just match around the periods.

    Since the web site sample include a boundary I'm guessing they aren't implied with is the norm.

    try

    ^[^.]+$

    I used the + instead of * because I would think the filename would at least have to be one character. If an empty string is also a good match change the + back to a *

    LightningAlso beware that is regex would allow values that are illegal for file names so you'd have to come up with a stricter regex to check for allowable file naming characters.
     

    Michael 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  12-08-2006, 11:42 AM 25358 in reply to 25357

    Re: Find Files Without An Extension

    mash (my favorite TV show, BTW), you were 100% correct.  Worked perfectly. 

    Thank you for your quick and exact response!

  •  04-16-2008, 6:26 AM 41436 in reply to 25357

    Re: Find Files Without An Extension

    mash:

    Since the web site sample include a boundary I'm guessing they aren't implied with is the norm. Try:

    ^[^.]+$

    I used the + instead of * because I would think the filename would at least have to be one character. If an empty string is also a good match change the + back to a *

    LightningAlso beware that is regex would allow values that are illegal for file names so you'd have to come up with a stricter regex to check for allowable file naming characters.

    How would a regex that matches any file name without an valid extension look?
  •  04-16-2008, 11:35 AM 41446 in reply to 41436

    Re: Find Files Without An Extension

    SignInName:
    mash:

    Since the web site sample include a boundary I'm guessing they aren't implied with is the norm. Try:

    ^[^.]+$

    I used the + instead of * because I would think the filename would at least have to be one character. If an empty string is also a good match change the + back to a *

    LightningAlso beware that is regex would allow values that are illegal for file names so you'd have to come up with a stricter regex to check for allowable file naming characters.

    How would a regex that matches any file name without an valid extension look?

    Define valid extension. Like the previous warning you'd have to learn what characters are not allowed in an extension and check for those values

    Pseudo Regex :

    ^(filenameRegex)\.(invalidExtension)$ 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  04-16-2008, 7:33 PM 41456 in reply to 41446

    Re: Find Files Without An Extension

    To add to Michael's warning, also consider what is a valid character for a filename. For some of the programming I'm doing at home right now, I am dealing with devices with names such as "/dev/cu.usbserialHDKLAJ" where the dot is a part of the device name. Such situations make it hard to define exactly where the file extension starts. I've seen some definitions that look for the last dot in the name that is not followed by a slash (forward or back depending on your OS).

    Susan 

View as RSS news feed in XML