Search code examples
regexpathfile-extension

Regex check if a file has any extension


I am looking for a regex to test if a file has any extension. I define it as: file has an extension if there is no slashes present after the last ".". The slashes are always backslashes.

I started with this regex

.*\..*[^\\]

Which translates to

.*          Any char, any number of repetitions 
\.          Literal .
.*          Any char, any number of repetitions 
[^\\]       Any char that is NOT in a class of [single slash]

This is my test data (excluding ##, which is my comments)

\path\foo.txt            ## I only want to capture this line
\pa.th\foo               ## But my regex also captures this line <-- PROBLEM HERE
\path\foo                ## This line is correctly filtered out

What would be a regex to do this?


Solution

  • Your solution is almost correct. Use this:

    ^.*\.[^\\]+$
    

    Sample at rubular.