正则表达式匹配有效的命名空间名称

我以前曾经问过这个问题,但是我试过谷歌,但没有找到答案。 也许我使用了错误的关键字

是否可以使用正则表达式来匹配有效的C#名称空间名称?


更新:

感谢大家的答案和研究! 这个问题比我预期的要复杂得多。 正如Oscar Mederos和Joey指出的那样,有效的命名空间不能包含C#保留关键字,并且可以包含比拉丁字母多得多的Unicode字符。

但是我当前的项目只需要在语法上validation名称空间。 所以我接受了primfaktor的答案,但我对所有答案都赞不绝口。

对我来说,这有效:

^using (@?[a-z_A-Z]\w+(?:\.@?[a-z_A-Z]\w+)*);$ 

它使用C#中的行进行匹配,并在第一个(也是唯一的)匹配组中返回完整的命名空间。 您可能希望删除^$以允许缩进和尾随注释。

RegExr上的示例。

我知道问题是如何使用正则表达式validation命名空间,但另一种方法是让编译器完成工作。 我不确定我在这里得到的100%的错误,它确实工作得很好。 我为我目前正在工作的项目创建了这个ValidationRule:

 using System.CodeDom.Compiler; using System.Windows.Controls; using Microsoft.CSharp; using System.Text.RegularExpressions; namespace Com.Gmail.Birklid.Ray.CodeGeneratorTemplateDialog { public class NamespaceValidationRule : ValidationRule { public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { var input = value as string; if (string.IsNullOrWhiteSpace(value as string)) { return new ValidationResult(false, "A namespace must be provided."); } else if (this.doubleDot.IsMatch(input)) { return new ValidationResult(false, "'..' is not valid."); } var inputs = (value as string).Split('.'); foreach (var item in inputs) { if (!this.compiler.IsValidIdentifier(item)) { return new ValidationResult(false, string.Format(cultureInfo, "'{0}' is invalid.", item)); } } return ValidationResult.ValidResult; } private readonly CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp"); private readonly Regex doubleDot = new Regex("\\.\\."); } } 

如果您想知道字符串是否可以用作命名空间,您应该参考C#语言规范并查看validation命名空间的语法。

命名空间应该是由a分隔的identifiers序列. 。 例:

identifier
identifier.identifier
identifier.identifier.identifier
...

什么是identifier

available_identifier@any_identifier

available_identifierany_identifier但不能是该语言保留的keyword

any_identifier如下:

(_|letter)(letter|number)*

编辑:
我必须说这个正则表达式真的很复杂。 请记住,有必要检查是否使用了保留关键字,以下是保留关键字的列表:

抽象作为基本bool中断字节情况捕获字符检查类const继续十进制默认委托做双重其他​​枚举事件显式外部错误最后固定浮点数为foreach goto如果隐含在int接口内部是锁定长命名空间新的空对象操作符输出覆盖参数私有受保护的公共readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw try typeof uint ulong unchecked unsafe ushort using virtual void volatile while

难道你不能拆分validation,可能用C#或任何其他语言创建一个方法来validation它而不是只使用一个正则表达式?

说实话,我建议你做这两件事:

  1. 实现该语法的解析器(请参阅参考资料)。 您可以手动或使用ANTLR等工具来完成
  2. 实现一个方法,它接受你想要validation的字符串(让我们称之为str )并写一个文件,如:

     namespace str { class A {} } 

并尝试使用msbuild或任何C#编译器编译它。 如果它给出错误,那么你知道这个词不正确:)

这个怎么样…

 (?:[AZ][a-zA-Z0-9\._]+)+[a-z0-9_]