I wrote a simple regex to accept both local file paths ( C:\\some\\path\\to\\file
) and network locations ( \\\\0.0.0.0\\some\\path\\to\\file
). I have captured C:\\
and \\\\0.0.0.0\\
in separate named groups, <drive>
and <netloc>
accordingly.
Is it possible in C# to check which group is matched after a "global" match is found? I would like to set flags whether the directory given is local or network. To separate my groups I use the |
character.
I've found similiar questions, but they do not cover C# and certainly are not easily "transferable".
Yes, the Groups
also have a Success
property.
Regex rx=new Regex(@"^((?<drive>[a-z]:\\.*)|(?<netloc>\\\\[^\\]+\\.*))$",
RegexOptions.IgnoreCase|RegexOptions.CultureInvariant|RegexOptions.ExplicitCapture);
Match match = rx.Match(somePath);
if (match.Groups["drive"].Success) {
// drive match
} else if (match.Groups["netloc"].Success) {
// netloc match
} else {
// none of these groups matched
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.