Regex: confirm that the file name does not end with .doc

I am trying to verify that the file does not end with .doc.

I want to prevent the download of doc and docx files.

This one ^.*(?<!doc|docx|DOC|DOCX).*$seems correct, explanatory, but it does not go away.

i.e. test.jpg should be allowed ... test.doc should not ...

etc.

+3
source share
5 answers

Try removing .*at the end:

^.*(?<!doc|docx|DOC|DOCX)$

although I propose to do the opposite. You can create a regular expression that will match files ending in .doc, .docxetc., and if it matches, you know that this is an invalid file.

, @krookedking, \., -, doc, docx,...

+3
(?i).*\.docx?

, .

+2

, . , - .doCx .dOcX ..

, , , ( , )

#:

    static void Main(string[] args)
    {
        string correctFilename = "something.xlsx";
        Debug.Assert(IsValidFile(correctFilename));

        string wrongFilename = "something.docx";
        Debug.Assert(!IsValidFile(wrongFilename));

        string wrongFilename2 = "something.doc";
        Debug.Assert(!IsValidFile(wrongFilename2));
    }

    static bool IsValidFile(string filename)
    {
        string ext = Path.GetExtension(filename).ToLower();
        return ext != ".docx"
            && ext != ".doc";
    }
0

:

^.*(?<!\.(?i)docx?)$

"?" x , (? i) , Doc DocX.

0

You can use the look, not the look ( Search Rules). Look forward below, do what you want, including if it ends with docx. (if no .doc (x) file name exists, such as test.extraperiod.doc)

^.*\.(?!doc).*$

Case insensitive

^.*\.(?!(?i)doc).*$

This look can solve the problem test.extraperiod.doc

^((?!\.(?i)docx?).)*$
0
source

All Articles