The Problem
I'm using the following regular expression to check for valid file paths:
^(?:[a-zA-Z]\:\\|\\\\)([^\\\/\:\*\?\<\>\"\|]+(\\){0,1})+$
Using the test string V:\Sample Names\Libraries\DeveloperLib\DeveloperComDlgs\res
is recognized as valid. I can even add invalid characters to the beginning of the string without issues. However, when I add an invalid character towards the end of the string, the webpage freezes up from catastrophic backtracking.
What is causing this in this regex string?
Breaking Down the Regex
Full string: ^(?:[a-zA-Z]\:\\|\\\\)([^\\\/\:\*\?\<\>\"\|]+(\\){0,1})+$
First Group: (?:[a-zA-Z]\:\\|\\\\)
- Checks for either
- A capital or lowercase alphabetical letter followed by a colon and a backslash
- A double backslash
Second Group: ([^\\\/\:\*\?\<\>\"\|]+(\\){0,1})
- First Part:
[^\\\/\:\*\?\<\>\"\|]+
- Makes sure there are no illegal characters ( \ / : * ? < > " | )
- Second Part:
(\\){0,1}
- Checks for a backslash between sections as many times as necessary
I think it may be the {0, 1}
causing the issue since this allows for backtracking but I am not sure. Any thoughts?