I'm trying to have a regular expression in a case of if the user chose a hard drive (e.g. "C:\" drive).
I've tried:
Match reg = Regex.Match(location, @"/[A-Z][:][\\]/");
And:
Match reg = Regex.Match(location, "/[A-Z][:][\\]/");
The 1st line doesn't detect, the 2nd line ends with an exception: System.ArgumentException
Presumably, you want to check that the string is exactly something like C:\
, but not something like ABC:\\ and my dog
. You need the anchors ^
and $
:
^[A-Z]:\\$
In code:
foundMatch = Regex.IsMatch(yourstring, @"^[A-Z]:\\$");
Note that I have removed the brackets you had in [:]
and [\\]
(not necessary since in each of these cases we are matching a single literal character, not one character from a class of several possible characters).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With