-1

I have this Regex to Check for a valid Windows Disk path.

^[a-z]:\\?(?:[^\\/:*?""<>|\r\n]+\\)*[^\\/:*?""<>|\r\n]*(?:[^./:*?""<>])$

Here I am giving this last part so that we do not allow Ending with Decimal and Special Characters like it should not be like "d:\abc\def."

(?:[^./:*?""<>])

But now it is allowing a dot in the middle somewhere like

d:\sdsd.\

and not taking

d:

NOTE: I had picked this original Regex from somewhere on the internet.

@"^[a-z]:\\(?:[^\\/:*?""<>|\r\n]+\\)*[^\\/:*?""<>|\r\n]*$"
BeeGees
  • 191
  • 1
  • 7

1 Answers1

1

This is what I came up with:

   ^[a-z]:\\?(?:(?:[^\\/:*?""<>|\r\n]+\\)*[^\\/:*?""<>|\r\n]*(?:[^.\\/:*?""<>])\\?)?$
A: ^^^^^^^^^^                                                                      ^
B:           ^^^                                                                 ^^
C:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   
D:                                                                            ^^^

A: ^[a-z]:\\? [...] $ same as before
B: (?: [...] )? new group to make everything after the letter and colon optional (because I assume you meant you wanted d: to match)
C: (?:[^\/:*?""<>|\r\n]+\\)*[^\/:*?""<>|\r\n]*(?:[^.\\\/:*?""<>]) same as before except the backslash is added to the prohibited end character class, because we are going to specify that manually with:
D: \\? Ending backslash, optional - but if there is one, it can match only here, since it's included in the prohibited character class.

  • you have got it perfect. Works Excellent. But can you explain what is the |[\r\n]+ and |\r\n part for. – BeeGees Feb 21 '20 at 07:21
  • @BeeGees the '\r' and '\n' parts are escape characters for special characters that can't normally be typed. The \r codes for a carriage return, and the \n codes for a newline. The | is just a character that is not allowed in the filename, so it is included with all the others that are not allowed. The + after the brackets [] mean to match any amount of qualifying characters, and at least one. – SpooqiSoftware Feb 21 '20 at 07:27
  • 1
    Ok Got it. I am marking your answer as Accepted Answer. Thanx for your help once again. – BeeGees Feb 21 '20 at 07:29