If you want to know if a string can be used as a namespace, you should refer to The C# Language Specifications and look at the grammar that validates the namespace.
The namespace should be a sequence of identifiers
separated by a .
. Example:
identifier
identifier.identifier
identifier.identifier.identifier
...
And what is an identifier
?
available_identifier
or @any_identifier
An available_identifier
is an any_identifier
but cannot be a keyword
reserved by the language.
any_identifier
is the following:
(_|letter)(letter|number)*
Edit:
I must say that this regex can be really really complicated. Take in count that it is necessary to check if no reserved keywords are used, and here is the list of the reserved keywords:
abstract as base bool break byte case
catch char checked class const
continue decimal default delegate do
double else enum event explicit extern
false finally fixed float for foreach
goto if implicit in int interface
internal is lock long namespace new
null object operator out override
params private protected public
readonly ref return sbyte sealed short
sizeof stackalloc static string struct
switch this throw true try typeof uint
ulong unchecked unsafe ushort using
virtual void volatile while
Can't you split the validation, maybe creating a method in C# or any other language to validate it instead of using only one regex?
To be honest, I suggest you any of those two things:
- Implement a parser of that grammar (see the reference). You can do it either by hand or using tools like ANTLR
Implement a method that takes the string you want to validate (let's call it str
) and write a file like:
namespace str
{
class A {}
}
and try to compile it :)
using msbuild or any C# compiler. If it gives an error, then you know that word is not correct :)