I've learned a lot about Regex since my last post here two years ago.
I came up with this and thought I would contribute/share. It's not perfect, but works well for most cases.
Flexible File Path pattern
As far as I can tell, it follows normal Windows path syntax. I went to great lengths to prevent things like spaces immediately before or after backslash, dots before backslash, trailing dots/spaces, etc. This is the reason the needle is longer than some you will see.
It will not find simple "filename.txt" (intentionally), because that would produce too many false-positives. You can add this if you like by tacking the pattern to the end (another OR pattern).
Hope others find it useful (test and respond if you find an issue with it)
Code: Select all
v1Needle := "(\b[A-z]:)((?:\\(?:\.+ *)*(?:(([^\\\s./:*?""<>|\x00-\x1F]+)(?:[. ]+(?-1))*)+))+(?<![. ])\\?)|(?-2)?(?-3)|(?-4)\\?"
RegExMatch(haystack, v1Needle)
/*
It will obviously find patterns that are similar to file path patterns, so keep this in mind.
Examples of what it should support
All of the following with/without dots and spaces (in the proper places)
Supports patterns found anywhere on a line, not just at beginning of line
d:\ (with or without trailing \)
d:\dir\dir\filename.ext (most needles only support this pattern, and allow illegal chars)
\dir\dir\ (with or without prefix \, or trailing \, or filename.ext)
*/