I thought this question was asked before but I tried Google but didn’t find an answer. Maybe I used wrong keywords.
Is it possible to use regular expression to match valid C# namespace name?
Update:
Thanks everyone for your answers and research! This question is much more complex than I expected. As Oscar Mederos and Joey pointed out, a valid namespace cannot contain C# reserved keywords, and can contain a lot more Unicode characters than Latin letters.
But my current project only need to syntactically validate namespaces. So I accepted primfaktor‘s answer, but I upvoted all answers.
For me, this worked:
It matches using lines in C# and returns the complete namespace in the first (and only) match group. You may want to remove
^and$to allow for indentation and trailing comments.Example on RegExr.