简体   繁体   English

正则表达式的命名空间不起作用

[英]Namespace of Regex won't work

Im refurbishing some old code that used to work on .NET Framework 3.5 to make it work on .NET Framework 4 using C#. 我翻新了一些过去在.NET Framework 3.5上工作的旧代码,使其可以在.NET Framework 4上使用C#。

The following Regex used to work fine with version 3.5 but doesn't work anymore for some strange reason. 以下正则表达式曾经与3.5版本一起正常工作但由于某些奇怪的原因不再起作用。

public static readonly Regex ChatColorRegex = new Regex("\\|c[A-Za-z0-9]{6,8}"),
                                 ChatLinkRegex = new Regex("\\|H.*?\\|h");

I have added the 'using System.Text.RegularExpressions' at the top of my file, but the following error rises: 'The type or namespace RegularExpressions does not exist in the namespace System.Text. 我在我的文件顶部添加了'using System.Text.RegularExpressions',但是出现了以下错误:'类型或名称空间RegularExpressions在命名空间System.Text中不存在。

I've googled about that and read that you have to add a Reference to System.Text.RegularExpressions in Visual Studio. 我已经用Google搜索了这一点,并且读到你必须在Visual Studio中添加对System.Text.RegularExpressions的引用。 However, when i did, i couldn't find System.Text.RegularExpressions in the list of References i could add. 但是,当我这样做时,我无法在我可以添加的References列表中找到System.Text.RegularExpressions。

I'm using Visual Studio 2012. 我正在使用Visual Studio 2012。

Could anyone tell me what im doing wrong, or forget to read? 任何人都可以告诉我我做错了什么,或忘记阅读?

The Regex class is still in the System.Text.RegularExpression namespace. Regex类仍在System.Text.RegularExpression命名空间中。 The class is in the System assembly. 该类位于系统程序集中。

If you check your project references in Solution Explorer, you should see a reference to the System assembly. 如果在解决方案资源管理器中检查项目引用,则应该看到对系统程序集的引用。 Check the properties of that reference to see what .NET Framework version is being used for the System assembly reference. 检查该引用的属性,以查看用于系统程序集引用的.NET Framework版本。 It should match the .NET Framework version you selected for the "Target framework" in the project properties (Application tab). 它应该与您为项目属性(“应用程序”选项卡)中的“目标框架”选择的.NET Framework版本相匹配。

Edit: The Regex class is in the System.Text.RegularExpressions namespace, not System.Text. 编辑:Regex类位于System.Text.RegularExpressions命名空间中,而不是System.Text。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM