Tuesday, July 14, 2015

Regular expression for validating file name ends with .doc or docx or pdf

Expression:  ^.+\.(?:(?:[dD][oO][cC][xX]?)|(?:[pP][dD][fF]))$

Explanation:

                   ^           = beginning of string

                   .+          = at least one character (any character)

                  \.            = dot ('.')

                (?:pattern) = match the pattern without storing the match)

                [dD]        = any character in the set ('d' or 'D')

                [xX]?       = any character in the set or none

              ('x' may be missing so 'doc' or 'docx' are both accepted)

                  |           = either the previous or the next pattern

                   $          = end of matched string

No comments:

Post a Comment